<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    On 25/05/2011 11:39, Yoni Tsafir wrote:
    <blockquote
      cite="mid:BANLkTikNcvKRgk_UPgkvKAq-r0V7k0_=pA@mail.gmail.com"
      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:&nbsp;</div>
        <div>
          <font class="Apple-style-span" 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>
          <meta http-equiv="content-type" content="text/html;
            charset=ISO-8859-1">
          <font class="Apple-style-span" face="'courier new', monospace">@patch("package.MyClassName")</font></div>
        <div><font class="Apple-style-span" face="'courier new',
            monospace">def test_something(self, my_mock):</font></div>
        <div><font class="Apple-style-span" face="'courier new',
            monospace">&nbsp; &nbsp; my_mock.my_method.return_value = "xyz"</font></div>
        <div><br>
        </div>
        <div>When in code I run:</div>
        <div><font class="Apple-style-span" 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>
    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.<br>
    <br>
    @patch('package.MyClassName')<br>
    def test_something(self, my_mock):<br>
    &nbsp;&nbsp;&nbsp; instance_mock = my_mock.return_value<br>
    &nbsp;&nbsp;&nbsp; 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
      cite="mid:BANLkTikNcvKRgk_UPgkvKAq-r0V7k0_=pA@mail.gmail.com"
      type="cite">
      <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>
      <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
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>