<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 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=utf-8"><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"> 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><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>