<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 02/11/2010 17:02, WW wrote:
<blockquote
cite="mid:AANLkTi=7EpD+s8h6WYEuHmAFhaun8qMxGvcE5bwweZGe@mail.gmail.com"
type="cite">You may have overlooked this from my earlier e-mail:<br>
<br>
Here's a small test case that produces the error in Python 2.6,
CentOS 5.5:<br>
</blockquote>
<br>
Ah, I did overlook that. Thanks, that's perfect.<br>
<br>
Your usage below looks fine, so this is almost certainly a bug in
mocksignature. Thanks for reporting it. I should be able to get it
fixed quickly. A beta 4 of 0.7.0 is due out soon.<br>
<br>
It's actually quite a big release and the biggest task for the final
release is updating the docs examples, which could take a while. :-(<br>
<br>
I'll report back here when I have identified (and hopefully solved)
the issue.<br>
<br>
All the best,<br>
<br>
Michael Foord<br>
<br>
<blockquote
cite="mid:AANLkTi=7EpD+s8h6WYEuHmAFhaun8qMxGvcE5bwweZGe@mail.gmail.com"
type="cite"><br>
<div style="font-family: monospace;">
<ol>
<li>
<div>from mock import patch</div>
</li>
<li>
<div> </div>
</li>
<li>
<div>class tc<span>(</span>object<span>)</span>:</div>
</li>
<li>
<div> def meth<span>(</span>a, b, c<span>)</span>:</div>
</li>
<li>
<div> pass</div>
</li>
<li>
<div> </div>
</li>
<li>
<div>g = tc<span>(</span><span>)</span></div>
</li>
<li>
<div> </div>
</li>
<li>
<div>@patch.object<span>(</span>g, 'meth',
mocksignature=True<span>)</span></div>
</li>
<li>
<div>def test_g<span>(</span>patched<span>)</span>:</div>
</li>
<li>
<div> g.meth<span>(</span><span>1</span><span>)</span></div>
</li>
<li>
<div> </div>
</li>
<li>
<div>test_g<span>(</span><span>)</span></div>
</li>
</ol>
</div>
<br>
<br>
<div class="gmail_quote">On Tue, Nov 2, 2010 at 1:00 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>
<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 16:57, WW wrote:
<blockquote type="cite">Replacing "@patch.object(g,
'meth', mocksignature=True)" with "@patch('%s.g.meth' %
__name__, mocksignature=True)" produces the same error.
Am I using this wrong?<br>
</blockquote>
<br>
</div>
Is this the same traceback you posted earlier?
<div class="im"><br>
<br>
'SentinelObject' object has no attribute '__call__'<br>
<br>
</div>
Can you reproduce this with a minimal example? I've never
seen that particular issue before.
<div class="im"><br>
<br>
<blockquote type="cite"><br>
Thanks for the clarification regarding spec vs
mocksignature. So if I wanted to do both (a mock class
that raises attribute errors when invalid attributes are
accessed, but actual method attributes return methods
that enforce the correct number of parameters), do I
have to set that up manually, or does spec use
mocksignature internally when returning attribute
values?<br>
<br>
</blockquote>
<br>
</div>
Using mocksignature changes the way that you access the
mock. As the methods are replaced with 'real function
objects' not mock objects as they normally are, you have to
do it explicitly. You could create a utility function to
auto patch out a mock with mocked-signatures.<br>
<br>
spec is purely about checking attribute access is valid.
<div>
<div class="h5"><br>
<br>
All the best,<br>
<br>
Michael Foord<br>
<br>
<blockquote type="cite">Thanks again...<br>
<br>
<div class="gmail_quote">On Tue, Nov 2, 2010 at 12:49
PM, Michael Foord <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:fuzzyman@voidspace.org.uk"
target="_blank">fuzzyman@voidspace.org.uk</a>></span>
wrote:<br>
<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> On 02/11/2010 16:43, WW wrote:
<blockquote 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"
target="_blank">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> 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><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>
</div>
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>
...
<div><br>
<br>
<br>
<blockquote type="cite">
<div class="gmail_quote">
<div bgcolor="#ffffff" text="#000000">
<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>
<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><br>
</div>
</div>
</blockquote>
<div bgcolor="#ffffff" text="#000000">
<div><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>
</div>
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.
<div>
<div><br>
<br>
All the best,<br>
<br>
Michael Foord<br>
<br>
<blockquote type="cite">
<div class="gmail_quote">
<div bgcolor="#ffffff" text="#000000">
<div> </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>
<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><br>
Here's a small test case that
produces the error in Python 2.6,
CentOS 5.5:<br>
<br>
<div style="font-family: monospace;">
<ol>
<li>
<div>from mock import patch</div>
</li>
<li>
<div> </div>
</li>
<li>
<div>class tc<span>(</span>object<span>)</span>:</div>
</li>
<li>
<div> def meth<span>(</span>a,
b, c<span>)</span>:</div>
</li>
<li>
<div> pass</div>
</li>
<li>
<div> </div>
</li>
<li>
<div>g = tc<span>(</span><span>)</span></div>
</li>
<li>
<div> </div>
</li>
<li>
<div>@patch.object<span>(</span>g,
'meth', mocksignature=True<span>)</span></div>
</li>
<li>
<div>def test_g<span>(</span>patched<span>)</span>:</div>
</li>
<li>
<div> g.meth<span>(</span><span>1</span><span>)</span></div>
</li>
<li>
<div> </div>
</li>
<li>
<div>test_g<span>(</span><span>)</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> <br>
All the best,<br>
<br>
Michael Foord<br>
</div>
</div>
</blockquote>
<div bgcolor="#ffffff" text="#000000">
<div><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>
<div>What am I missing here?<br>
<br>
Thanks for your help.</div>
</div>
<pre><fieldset></fieldset>
_______________________________________________
testing-in-python mailing list
<div><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> <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>
</div>
</div>
<pre cols="72"><div><div>--
<a moz-do-not-send="true" href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a></div></div>
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>
</div>
</blockquote>
</div>
<br>
</blockquote>
<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>
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>
</div>
</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>