[TIP] [Python 2] Objects' mocked magic method returns another mock
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
> 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,
> Date: Sat, 1 Oct 2011 11:10:26 -0500
> From: "Matthew J. Morrison" <mattjmorrison at mattjmorrison.com
> Subject: Re: [TIP] [Python 2] Objects' mocked magic method returns
> another mock
> 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 <
> > 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
> > 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
> > Thanks :)
> > Alexandre
> > _______________________________________________
> > testing-in-python mailing list
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
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...
More information about the testing-in-python