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