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

Michael Foord michael at voidspace.org.uk
Sun Oct 2 16:47:31 PDT 2011


On 02/10/2011 05:44, Alexandre Gravier wrote:
> Thank you, you are right, this solves my problem.
>
> I assumed that the MagicMock default return values applied 
> (http://www.voidspace.org.uk/python/mock/magicmock.html#magic-mock) 
> without actually trying manually.
>

You're not actually using a MagicMock here, you're patching out the 
__int__ method on a class with another Mock. In 0.8 patch will use a 
MagicMock by default - but that still won't help your case. It isn't the 
magic method of the mock that is being called - the mock is called 
directly when TestObject.__int__ is called.

As Matthew said, the fix is to ensure your mock returns an integer. You 
can do this in the patch call:

     PATCH = patch.object(TestObject, "__int__", return_value=3)

All the best,

Michael Foord

> Cheers
> Alexandre
>
>     Date: Sat, 1 Oct 2011 11:10:26 -0500
>     From: "Matthew J. Morrison" <mattjmorrison at mattjmorrison.com
>     <javascript:;>>
>     Subject: Re: [TIP] [Python 2] Objects' mocked magic method returns
>            another mock
>     To: "testing-in-python at lists.idyll.org <javascript:;>"
>     <testing-in-python at lists.idyll.org <javascript:;>>
>     Message-ID:
>     <CAKUA1R7bdDkPWoNYjcRjTMRp99JRpU5KBk2Tq8Cg6iZ-D+5sdQ at mail.gmail.com <javascript:;>>
>     Content-Type: text/plain; charset="iso-8859-1"
>
>     You'll need to make sure that the __int__ method returns an int.
>
>     after your PATCH.start() try adding
>
>     T1.__int__.return_value = 1
>
>     On Sat, Oct 1, 2011 at 5:12 AM, Alexandre Gravier <
>     alexandre.gravier at gmail.com <javascript:;>> wrote:
>
>     > 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()
>     > PATCH.start()
>     > 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
>     > yadda
>     >
>     > Does anyone have some idea to help me understand what I'm doing
>     wrong?
>     >
>     > Thanks :)
>     > Alexandre
>     >
>     > _______________________________________________
>     > testing-in-python mailing list
>
>
>
> _______________________________________________
> 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/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20111003/19b93596/attachment.htm>


More information about the testing-in-python mailing list