<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 02/11/2010 16:43, WW wrote:
<blockquote
cite="mid:AANLkTimf0KqYs-yfBh=vwZEDUqEp8LD0_J3h7MKO=dqE@mail.gmail.com"
type="cite">On Tue, Nov 2, 2010 at 12:17 PM, Michael Foord <span
dir="ltr"><<a moz-do-not-send="true"
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>
</div>
</div>
</div>
</blockquote>
<br>
You mean a global variable in the current module? If so then still
yes.<br>
<br>
@patch('%s.function' % __name__, mocksignature=True)<br>
def test_something(self, mockfunction):<br>
...<br>
<br>
<br>
<blockquote
cite="mid:AANLkTimf0KqYs-yfBh=vwZEDUqEp8LD0_J3h7MKO=dqE@mail.gmail.com"
type="cite">
<div class="gmail_quote">
<div bgcolor="#ffffff" text="#000000">
<div class="im">
<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>
</div>
</blockquote>
<br>
spec is not for mocking functions (mocksignature is). Spec is for
mocking out classes / objects to check that only methods /
attributes that exist on the spec object are used. Accessing other
attributes will raise an AttributeError.<br>
<br>
All the best,<br>
<br>
Michael Foord<br>
<br>
<blockquote
cite="mid:AANLkTimf0KqYs-yfBh=vwZEDUqEp8LD0_J3h7MKO=dqE@mail.gmail.com"
type="cite">
<div class="gmail_quote">
<div bgcolor="#ffffff" text="#000000">
<div class="im"> </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 moz-do-not-send="true" href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a>
<a moz-do-not-send="true" 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 moz-do-not-send="true" href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a></pre>
</div>
</div>
</blockquote>
</div>
<br>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">--
<a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/">http://www.voidspace.org.uk/</a>
READ CAREFULLY. By accepting and reading this email you agree,
on behalf of your employer, to release me from all obligations
and waivers arising from any and all NON-NEGOTIATED agreements,
licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap,
confidentiality, non-disclosure, non-compete and acceptable use
policies (”BOGUS AGREEMENTS”) that I have entered into with your
employer, its partners, licensors, agents and assigns, in
perpetuity, without prejudice to my ongoing rights and privileges.
You further represent that you have the authority to release me
from any BOGUS AGREEMENTS on behalf of your employer.
</pre>
</body>
</html>