<br><br><div class="gmail_quote">On Mon, Oct 3, 2011 at 4:10 PM, Gary Bernhardt <span dir="ltr">&lt;<a href="mailto:gary.bernhardt@gmail.com">gary.bernhardt@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

STUFF is created at class definition time, before your patches are<br>
run. The patches are successfully patching the method, but STUFF<br>
already holds a reference to the original, so that&#39;s what&#39;s getting<br>
called.<br></blockquote><div><br></div><div>Right, I was thinking over that and the only thing I can really do, then, is to instantiate Foo(), change the inner stuff to lists, and then manually replace foo.STUFF[0][0] with a mock object? That just seems extra ugly.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
--<br>
Gary<br>
<a href="http://destroyallsoftware.com" target="_blank">http://destroyallsoftware.com</a><br>
<div><div></div><div class="h5"><br>
<br>
<br>
On Mon, Oct 3, 2011 at 12:14 PM, Tom Davis &lt;<a href="mailto:tom@recursivedream.com">tom@recursivedream.com</a>&gt; wrote:<br>
&gt; First off, sorry for the vague term &quot;referenced methods&quot;; I don&#39;t know the<br>
&gt; proper term for this. Basically, I cannot seem to mock a class method by<br>
&gt; name if it is being called as a reference from another attribute. If that<br>
&gt; still makes no sense, here&#39;s an example:<br>
&gt;<br>
&gt; import mock<br>
&gt;<br>
&gt; class Foo(object):<br>
&gt;     def meth(self):<br>
&gt;         return True<br>
&gt;     STUFF = ((meth, True),)<br>
&gt;     def do(self):<br>
&gt;         for m, r in self.STUFF:<br>
&gt;             assert m(self) == r<br>
&gt;<br>
&gt; f = Foo()<br>
&gt; f.do()<br>
&gt; with mock.patch.object(f, &#39;meth&#39;) as meth:<br>
&gt;     f.do()<br>
&gt;     assert meth.called<br>
&gt; with mock.patch(&#39;__main__.Foo.meth&#39;) as meth:<br>
&gt;     f.do()<br>
&gt;     assert meth.called<br>
&gt;<br>
&gt; This example is a bit contrived, but should get the point across. I believe<br>
&gt; I understand why the former doesn&#39;t work (I need to explicitly call &quot;meth&quot;<br>
&gt; with &quot;self&quot;, thus it isn&#39;t an instance attribute), but I don&#39;t understand<br>
&gt; why the latter doesn&#39;t work, either...<br>
</div></div>&gt; _______________________________________________<br>
&gt; testing-in-python mailing list<br>
&gt; <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
&gt; <a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
&gt;<br>
&gt;<br>
</blockquote></div><br>