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

Alexandre Gravier alexandre.gravier at gmail.com
Sat Oct 1 21:44:34 PDT 2011


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.

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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20111002/95df4c44/attachment.htm>


More information about the testing-in-python mailing list