[TIP] I don't get it?

Michael Foord fuzzyman at voidspace.org.uk
Tue Nov 2 09:13:35 PDT 2010


On 02/11/2010 15:55, vanderson.mota at gmail.com wrote:
> why don't you use:
>
> @patch('somemodule.somefunction', spec=True)
> def test_spam(self, somefunctionmock):
>     ... some code ...
>     somefunctionmock.assert_called_with(expected_param)
>
This is the generally correct way to use patch to replace a function in 
a module.  mocksignature=True instead of spec=True may be useful too. As 
mocksignature replaces the original with a generated function object 
that delegates to a mock using both is superfluous.

The original poster didn't give quite enough detail to diagnose why 
"patch.object" didn't work for him (it *should* still work), but we'll 
see if this solution is sufficient for him.

The traceback *looks* like a bug (where is that sentinel coming from?). 
I'll try and reproduce it,  but I may need more details.

All the best,

Michael Foord


> cheers!
>
> 2010/11/2 WW<teetee at gmail.com>:
>> Hello,
>>
>> 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.
>>
>> 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:
>>
>> @patch.object(somemodule, 'somemethod', spec=True)
>>
>> It doesn't seem to have any effect; I can call somemodule.somemethod with
>> any combination of invalid arguments and no exceptions are thrown.  However,
>> when I do:
>>
>> @patch.object(somemodule, 'somemethod', mocksignature=True)
>>
>> I get:
>>
>> Traceback (most recent call last):
>>    File "/usr/lib/python2.6/site-
>> packages/mock-0.7.0b3-py2.6.egg/mock.py", line 485, in patched
>>      arg = patching.__enter__()
>>    File "/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py",
>> line 536, in __enter__
>>      new_attr = mocksignature(original, new)
>>    File "/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py",
>> line 140, in mocksignature
>>      signature, func = _getsignature(func, skipfirst)
>>    File "/usr/lib/python2.6/site-packages/mock-0.7.0b3-py2.6.egg/mock.py",
>> line 87, in _getsignature
>>      func = func.__call__
>> AttributeError: 'SentinelObject' object has no attribute '__call__'
>>
>> What am I missing here?
>>
>> Thanks for your help.
>> _______________________________________________
>> testing-in-python mailing list
>> testing-in-python at lists.idyll.org
>> http://lists.idyll.org/listinfo/testing-in-python
>>
>>
>
>


-- 
http://www.voidspace.org.uk/




More information about the testing-in-python mailing list