On Tue, Nov 2, 2010 at 12:17 PM, Michael Foord <span dir="ltr"><<a href="mailto:fuzzyman@voidspace.org.uk">fuzzyman@voidspace.org.uk</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div bgcolor="#ffffff" text="#000000"><div class="im">
On 02/11/2010 15:34, WW wrote:
<blockquote type="cite">Hello,<br>
<br>
This is my first time using the python mock library and I'm a
little confused. I'd like to provide some guarantees that my
mocks are being called with the correct number of arguments. The
documentation seems to indicate there are two ways to do this,
"spec" and "mocksignature", but it's a little unclear to me what
the difference is supposed to be between them.<br>
<br>
I find myself using the @patch.object decorator almost all the
time, because the modules I'm testing use a lot of top-level
functions from modules they've imported. When I do something like
this:<br>
<br>
@patch.object(somemodule, 'somemethod', spec=True)<br>
<br>
</blockquote>
<br></div>
You should still be able to use patch with a named function (as a
string). See the other replies for an example.</div></blockquote><div bgcolor="#ffffff" text="#000000"><div class="im"><br>Does this work if I'm trying to patch a global variable rather than a function in another module?<br>
<br>
</div></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div bgcolor="#ffffff" text="#000000"><div class="im"><blockquote type="cite">
It doesn't seem to have any effect; I can call
somemodule.somemethod with any combination of invalid arguments
and no exceptions are thrown. </blockquote>
<br></div>
Using spec doesn't protect you against being called with invalid
arguments. You should get an error when you validate that the calls
were made correctly when you call 'assert_called_with'.<div class="im"><br>
</div></div></blockquote><div bgcolor="#ffffff" text="#000000"><div class="im"><br>assert_called_with is fine if I know the exact values I want the function to be called with, but I'm just trying to make sure that a function was called with the correct number of arguments. If spec can't do this, then what is spec supposed to be used for? I'm still unclear as to what the difference is between spec and mocksignature and why both exist.<br>
</div></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div bgcolor="#ffffff" text="#000000"><div class="im"><blockquote type="cite">
However, when I do:<br>
<br>
@patch.object(somemodule, 'somemethod', mocksignature=True)<br>
<br>
I get:<br>
<br>
Traceback (most recent call last):<br>
File "/usr/lib/python2.6/site-
<div>packages/mock-0.7.0b3-py2.6.egg/mock.py", line 485,
in patched<br>
arg = patching.__enter__()<br>
File
"/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py",
line 536, in __enter__<br>
new_attr = mocksignature(original, new)<br>
File
"/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py",
line 140, in mocksignature<br>
signature, func = _getsignature(func, skipfirst)<br>
File
"/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py",
line 87, in _getsignature<br>
func = func.__call__<br>
AttributeError: 'SentinelObject' object has no attribute
'__call__'<br>
<br>
</div>
</blockquote>
<br></div>
This is weird. The traceback implies that you are trying to replace
a sentinel object using mocksignature (and sentinels don't have
signatures to mock). Either that or it is a bug. I'll create a
simple test case here (but this functionality *is* tested), but it
looks like something is not quite setup how you expect.</div></blockquote><div bgcolor="#ffffff" text="#000000"><div class="im"><br>Here's a small test case that produces the error in Python 2.6, CentOS 5.5:<br><br>
<div class="raw" id="source" style="font-family: monospace;"><ol><li class="li1"><div class="de1">from mock import patch</div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">class tc<span class="br0">(</span>object<span class="br0">)</span>:</div>
</li><li class="li1"><div class="de1"> def meth<span class="br0">(</span>a, b, c<span class="br0">)</span>:</div></li><li class="li2"><div class="de2"> pass</div></li><li class="li1"><div class="de1"> </div></li>
<li class="li1"><div class="de1">g = tc<span class="br0">(</span><span class="br0">)</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">@patch.object<span class="br0">(</span>g, 'meth', mocksignature=True<span class="br0">)</span></div>
</li><li class="li2"><div class="de2">def test_g<span class="br0">(</span>patched<span class="br0">)</span>:</div></li><li class="li1"><div class="de1"> g.meth<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span></div>
</li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">test_g<span class="br0">(</span><span class="br0">)</span></div></li></ol></div><br></div></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div bgcolor="#ffffff" text="#000000"><div class="im">
<br>
All the best,<br>
<br>
Michael Foord<br>
</div></div></blockquote><div bgcolor="#ffffff" text="#000000"><div class="im"><br>Likewise.<br>
</div></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div bgcolor="#ffffff" text="#000000"><blockquote type="cite"><div class="im">
<div>What am I missing here?<br>
<br>
Thanks for your help.</div>
</div><pre><fieldset></fieldset>
_______________________________________________
testing-in-python mailing list
<div class="im"><a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a>
</div></pre>
</blockquote><div class="im">
<br>
<br>
<pre cols="72">--
<a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a></pre>
</div></div>
</blockquote></div><br>