python/mock-1.0.0/html/sentinel.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1
michael@0 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
michael@0 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
michael@0 4
michael@0 5
michael@0 6 <html xmlns="http://www.w3.org/1999/xhtml">
michael@0 7 <head>
michael@0 8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
michael@0 9
michael@0 10 <title>Sentinel &mdash; Mock 1.0.0 documentation</title>
michael@0 11
michael@0 12 <link rel="stylesheet" href="_static/nature.css" type="text/css" />
michael@0 13 <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
michael@0 14
michael@0 15 <script type="text/javascript">
michael@0 16 var DOCUMENTATION_OPTIONS = {
michael@0 17 URL_ROOT: '',
michael@0 18 VERSION: '1.0.0',
michael@0 19 COLLAPSE_INDEX: false,
michael@0 20 FILE_SUFFIX: '.html',
michael@0 21 HAS_SOURCE: true
michael@0 22 };
michael@0 23 </script>
michael@0 24 <script type="text/javascript" src="_static/jquery.js"></script>
michael@0 25 <script type="text/javascript" src="_static/underscore.js"></script>
michael@0 26 <script type="text/javascript" src="_static/doctools.js"></script>
michael@0 27 <link rel="top" title="Mock 1.0.0 documentation" href="index.html" />
michael@0 28 <link rel="next" title="Mocking Magic Methods" href="magicmock.html" />
michael@0 29 <link rel="prev" title="Helpers" href="helpers.html" />
michael@0 30 </head>
michael@0 31 <body>
michael@0 32 <div class="related">
michael@0 33 <h3>Navigation</h3>
michael@0 34 <ul>
michael@0 35 <li class="right" style="margin-right: 10px">
michael@0 36 <a href="genindex.html" title="General Index"
michael@0 37 accesskey="I">index</a></li>
michael@0 38 <li class="right" >
michael@0 39 <a href="magicmock.html" title="Mocking Magic Methods"
michael@0 40 accesskey="N">next</a> |</li>
michael@0 41 <li class="right" >
michael@0 42 <a href="helpers.html" title="Helpers"
michael@0 43 accesskey="P">previous</a> |</li>
michael@0 44 <li><a href="index.html">Mock 1.0.0 documentation</a> &raquo;</li>
michael@0 45 </ul>
michael@0 46 </div>
michael@0 47
michael@0 48 <div class="document">
michael@0 49 <div class="documentwrapper">
michael@0 50 <div class="bodywrapper">
michael@0 51 <div class="body">
michael@0 52
michael@0 53 <div class="section" id="sentinel">
michael@0 54 <h1>Sentinel<a class="headerlink" href="#sentinel" title="Permalink to this headline">¶</a></h1>
michael@0 55 <dl class="data">
michael@0 56 <dt id="mock.sentinel">
michael@0 57 <tt class="descname">sentinel</tt><a class="headerlink" href="#mock.sentinel" title="Permalink to this definition">¶</a></dt>
michael@0 58 <dd><p>The <tt class="docutils literal"><span class="pre">sentinel</span></tt> object provides a convenient way of providing unique
michael@0 59 objects for your tests.</p>
michael@0 60 <p>Attributes are created on demand when you access them by name. Accessing
michael@0 61 the same attribute will always return the same object. The objects
michael@0 62 returned have a sensible repr so that test failure messages are readable.</p>
michael@0 63 </dd></dl>
michael@0 64
michael@0 65 <dl class="data">
michael@0 66 <dt id="mock.DEFAULT">
michael@0 67 <tt class="descname">DEFAULT</tt><a class="headerlink" href="#mock.DEFAULT" title="Permalink to this definition">¶</a></dt>
michael@0 68 <dd><p>The <cite>DEFAULT</cite> object is a pre-created sentinel (actually
michael@0 69 <cite>sentinel.DEFAULT</cite>). It can be used by <a class="reference internal" href="mock.html#mock.Mock.side_effect" title="mock.Mock.side_effect"><tt class="xref py py-attr docutils literal"><span class="pre">side_effect</span></tt></a>
michael@0 70 functions to indicate that the normal return value should be used.</p>
michael@0 71 </dd></dl>
michael@0 72
michael@0 73 <div class="section" id="sentinel-example">
michael@0 74 <h2>Sentinel Example<a class="headerlink" href="#sentinel-example" title="Permalink to this headline">¶</a></h2>
michael@0 75 <p>Sometimes when testing you need to test that a specific object is passed as an
michael@0 76 argument to another method, or returned. It can be common to create named
michael@0 77 sentinel objects to test this. <cite>sentinel</cite> provides a convenient way of
michael@0 78 creating and testing the identity of objects like this.</p>
michael@0 79 <p>In this example we monkey patch <cite>method</cite> to return
michael@0 80 <cite>sentinel.some_object</cite>:</p>
michael@0 81 <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">real</span> <span class="o">=</span> <span class="n">ProductionClass</span><span class="p">()</span>
michael@0 82 <span class="gp">&gt;&gt;&gt; </span><span class="n">real</span><span class="o">.</span><span class="n">method</span> <span class="o">=</span> <span class="n">Mock</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;method&quot;</span><span class="p">)</span>
michael@0 83 <span class="gp">&gt;&gt;&gt; </span><span class="n">real</span><span class="o">.</span><span class="n">method</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">some_object</span>
michael@0 84 <span class="gp">&gt;&gt;&gt; </span><span class="n">result</span> <span class="o">=</span> <span class="n">real</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
michael@0 85 <span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="n">result</span> <span class="ow">is</span> <span class="n">sentinel</span><span class="o">.</span><span class="n">some_object</span>
michael@0 86 <span class="gp">&gt;&gt;&gt; </span><span class="n">sentinel</span><span class="o">.</span><span class="n">some_object</span>
michael@0 87 <span class="go">sentinel.some_object</span>
michael@0 88 </pre></div>
michael@0 89 </div>
michael@0 90 </div>
michael@0 91 </div>
michael@0 92
michael@0 93
michael@0 94 </div>
michael@0 95 </div>
michael@0 96 </div>
michael@0 97 <div class="sphinxsidebar">
michael@0 98 <div class="sphinxsidebarwrapper">
michael@0 99 <h3><a href="index.html">Table Of Contents</a></h3>
michael@0 100 <ul>
michael@0 101 <li><a class="reference internal" href="#">Sentinel</a><ul>
michael@0 102 <li><a class="reference internal" href="#sentinel-example">Sentinel Example</a></li>
michael@0 103 </ul>
michael@0 104 </li>
michael@0 105 </ul>
michael@0 106
michael@0 107 <h4>Previous topic</h4>
michael@0 108 <p class="topless"><a href="helpers.html"
michael@0 109 title="previous chapter">Helpers</a></p>
michael@0 110 <h4>Next topic</h4>
michael@0 111 <p class="topless"><a href="magicmock.html"
michael@0 112 title="next chapter">Mocking Magic Methods</a></p>
michael@0 113 <h3>This Page</h3>
michael@0 114 <ul class="this-page-menu">
michael@0 115 <li><a href="_sources/sentinel.txt"
michael@0 116 rel="nofollow">Show Source</a></li>
michael@0 117 </ul>
michael@0 118 <div id="searchbox" style="display: none">
michael@0 119 <h3>Quick search</h3>
michael@0 120 <form class="search" action="search.html" method="get">
michael@0 121 <input type="text" name="q" />
michael@0 122 <input type="submit" value="Go" />
michael@0 123 <input type="hidden" name="check_keywords" value="yes" />
michael@0 124 <input type="hidden" name="area" value="default" />
michael@0 125 </form>
michael@0 126 <p class="searchtip" style="font-size: 90%">
michael@0 127 Enter search terms or a module, class or function name.
michael@0 128 </p>
michael@0 129 </div>
michael@0 130 <script type="text/javascript">$('#searchbox').show(0);</script>
michael@0 131 </div>
michael@0 132 </div>
michael@0 133 <div class="clearer"></div>
michael@0 134 </div>
michael@0 135 <div class="related">
michael@0 136 <h3>Navigation</h3>
michael@0 137 <ul>
michael@0 138 <li class="right" style="margin-right: 10px">
michael@0 139 <a href="genindex.html" title="General Index"
michael@0 140 >index</a></li>
michael@0 141 <li class="right" >
michael@0 142 <a href="magicmock.html" title="Mocking Magic Methods"
michael@0 143 >next</a> |</li>
michael@0 144 <li class="right" >
michael@0 145 <a href="helpers.html" title="Helpers"
michael@0 146 >previous</a> |</li>
michael@0 147 <li><a href="index.html">Mock 1.0.0 documentation</a> &raquo;</li>
michael@0 148 </ul>
michael@0 149 </div>
michael@0 150 <div class="footer">
michael@0 151 &copy; Copyright 2007-2012, Michael Foord &amp; the mock team.
michael@0 152 Last updated on Oct 07, 2012.
michael@0 153 Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
michael@0 154 </div>
michael@0 155 </body>
michael@0 156 </html>

mercurial