<div dir="ltr">Great!<div>That's a much better solution :)</div><div><br></div><div>I think you should put it in the "Further Examples" section of the website.</div><div><br></div><div>Thank you very much,</div>
<div>Yoni.</div><div><br><div class="gmail_quote">On Wed, May 25, 2011 at 9:26 PM, Michael Foord <span dir="ltr"><<a href="mailto:michael@voidspace.org.uk">michael@voidspace.org.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div bgcolor="#ffffff" text="#000000"><div class="im">
On 25/05/2011 11:39, Yoni Tsafir wrote:
<blockquote type="cite">
<div dir="ltr">
<div>Sorry for spamming with mock questions the last few days :)</div>
<div>Just doing some massive testing for complicated code.</div>
<div><br>
</div>
<div>I know that when I patch a specific method: </div>
<div>
<font face="'courier new', monospace">@patch("package.MyClassName.my_method",
new = Mock(return_value="xyz"))</font></div>
<div><br>
</div>
<div>Even new instances of MyClass will return "xyz" as
expected.</div>
<div><br>
</div>
<div>However, when I try:</div>
<div>
<font face="'courier new', monospace">@patch("package.MyClassName")</font></div>
<div><font face="'courier new',
monospace">def test_something(self, my_mock):</font></div>
<div><font face="'courier new',
monospace"> my_mock.my_method.return_value = "xyz"</font></div>
<div><br>
</div>
<div>When in code I run:</div>
<div><font face="'courier new',
monospace">MyClassName().my_method()</font></div>
<div><br>
</div>
<div>I get a new different Mock and not "xyz" as I would wish.</div>
</div>
</blockquote>
<br></div>
Right. When you mock MyClassName then the class is replaced with a
mock. Instances are created by *calling the class*. This means you
access the "mock instance" by looking at the return value of the
mocked class. e.g.<div class="im"><br>
<br>
@patch('package.MyClassName')<br>
def test_something(self, my_mock):<br></div>
instance_mock = my_mock.return_value<br>
instance_mock.my_method.return_value = 'xyz'<br>
<br>
mock autocreates a new mock to be the return value of any mock. So
you control the behaviour of the instances of mocked classes by
configuring my_mock.return_value.<br>
<br>
All the best,<br>
<br>
Michael Foord<br>
<br>
<blockquote type="cite"><div class="im">
<div dir="ltr">
<div><br>
</div>
<div>Now, it kinda makes sense to me, but I would like to know
if there's a way around it.</div>
<div>Couldn't really achieve this by mocking __init__ (or maybe
I did it wrong?)</div>
<div><br>
</div>
<div>
Thanks!</div>
<div>Yoni.</div>
<div>
<div><br>
</div>
</div>
</div>
</div><pre><fieldset></fieldset>
_______________________________________________
testing-in-python mailing list
<div class="im"><a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.org</a>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a>
</div></pre>
</blockquote>
<br>
<br>
<pre cols="72">--
<a href="http://www.voidspace.org.uk/" target="_blank">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 href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a>
</pre>
</div>
</blockquote></div><br></div></div>