<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
On 02/10/2011 05:44, Alexandre Gravier wrote:
<blockquote
cite="mid:CAOZahavrSChs_EDyytDRkDRSVov0ifp2tvgqGbSwzibU661O3A@mail.gmail.com"
type="cite">
<div>Thank you, you are right, this solves my problem. </div>
<div><br>
</div>
<div>I assumed that the MagicMock default return values applied (<a
moz-do-not-send="true"
href="http://www.voidspace.org.uk/python/mock/magicmock.html#magic-mock">http://www.voidspace.org.uk/python/mock/magicmock.html#magic-mock</a>)
without actually trying manually.</div>
<br>
</blockquote>
<br>
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. <br>
<br>
As Matthew said, the fix is to ensure your mock returns an integer.
You can do this in the patch call:<br>
<br>
PATCH = patch.object(TestObject, "__int__", return_value=3)<br>
<br>
All the best,<br>
<br>
Michael Foord<br>
<br>
<blockquote
cite="mid:CAOZahavrSChs_EDyytDRkDRSVov0ifp2tvgqGbSwzibU661O3A@mail.gmail.com"
type="cite">Cheers
<div>Alexandre<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
Date: Sat, 1 Oct 2011 11:10:26 -0500<br>
From: "Matthew J. Morrison" <<a moz-do-not-send="true"
href="javascript:;" onclick="_e(event, 'cvml',
'mattjmorrison@mattjmorrison.com')">mattjmorrison@mattjmorrison.com</a>><br>
Subject: Re: [TIP] [Python 2] Objects' mocked magic method
returns<br>
another mock<br>
To: "<a moz-do-not-send="true" href="javascript:;"
onclick="_e(event, 'cvml',
'testing-in-python@lists.idyll.org')">testing-in-python@lists.idyll.org</a>"<br>
<<a moz-do-not-send="true" href="javascript:;"
onclick="_e(event, 'cvml',
'testing-in-python@lists.idyll.org')">testing-in-python@lists.idyll.org</a>><br>
Message-ID:<br>
<<a moz-do-not-send="true" href="javascript:;"
onclick="_e(event, 'cvml',
'CAKUA1R7bdDkPWoNYjcRjTMRp99JRpU5KBk2Tq8Cg6iZ-D+5sdQ@mail.gmail.com')">CAKUA1R7bdDkPWoNYjcRjTMRp99JRpU5KBk2Tq8Cg6iZ-D+5sdQ@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="iso-8859-1"<br>
<br>
You'll need to make sure that the __int__ method returns an
int.<br>
<br>
after your PATCH.start() try adding<br>
<br>
T1.__int__.return_value = 1<br>
<br>
On Sat, Oct 1, 2011 at 5:12 AM, Alexandre Gravier <<br>
<a moz-do-not-send="true" href="javascript:;"
onclick="_e(event, 'cvml', 'alexandre.gravier@gmail.com')">alexandre.gravier@gmail.com</a>>
wrote:<br>
<br>
> Hi testers-in-python,<br>
><br>
> I am stuck on the following:<br>
><br>
> from mock import patch<br>
><br>
> class TestObject(object):<br>
> def __int__(self):<br>
> return 42<br>
><br>
> PATCH = patch.object(TestObject, "__int__")<br>
> T1 = TestObject()<br>
> PATCH.start()<br>
> print "int(T1) with patched class:", int(T1)<br>
><br>
> Output: [...]<br>
> TypeError: __int__ returned non-int (type Mock)<br>
><br>
> Am I understanding mock.patch.object incorrectly? I need
to temporarily<br>
> mock the __int__ macig method, so the patch should be
stop()able.<br>
> The platform is Python 2.7.2, linux, 32 bit, mock 0.7.2,
virtualenv, yadda<br>
> yadda<br>
><br>
> Does anyone have some idea to help me understand what I'm
doing wrong?<br>
><br>
> Thanks :)<br>
> Alexandre<br>
><br>
> _______________________________________________<br>
> testing-in-python mailing list<br>
<br>
</blockquote>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
testing-in-python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
<a class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
</pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">--
<a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/">http://www.voidspace.org.uk/</a>
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 <a class="moz-txt-link-freetext" href="http://www.sqlite.org/different.html">http://www.sqlite.org/different.html</a>
</pre>
</body>
</html>