[TIP] [Python 2] Objects' mocked magic method returns another mock

Alexandre Gravier alexandre.gravier at gmail.com
Sat Oct 1 03:12:53 PDT 2011

Hi testers-in-python,

I am stuck on the following:

from mock import patch

class TestObject(object):
    def __int__(self):
        return 42

PATCH = patch.object(TestObject, "__int__")
T1 = TestObject()
print "int(T1) with patched class:", int(T1)

Output: [...]
TypeError: __int__ returned non-int (type Mock)

Am I understanding mock.patch.object incorrectly? I need to temporarily mock
the __int__ macig method, so the patch should be stop()able.
The platform is Python 2.7.2, linux, 32 bit, mock 0.7.2, virtualenv, yadda

Does anyone have some idea to help me understand what I'm doing wrong?

Thanks :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20111001/301e7c3b/attachment.html>

More information about the testing-in-python mailing list