<br><br><div class="gmail_quote">On Mon, Oct 3, 2011 at 4:10 PM, Gary Bernhardt <span dir="ltr"><<a href="mailto:gary.bernhardt@gmail.com">gary.bernhardt@gmail.com</a>></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's what'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 <<a href="mailto:tom@recursivedream.com">tom@recursivedream.com</a>> wrote:<br>
> First off, sorry for the vague term "referenced methods"; I don't know the<br>
> proper term for this. Basically, I cannot seem to mock a class method by<br>
> name if it is being called as a reference from another attribute. If that<br>
> still makes no sense, here's an example:<br>
><br>
> import mock<br>
><br>
> class Foo(object):<br>
> def meth(self):<br>
> return True<br>
> STUFF = ((meth, True),)<br>
> def do(self):<br>
> for m, r in self.STUFF:<br>
> assert m(self) == r<br>
><br>
> f = Foo()<br>
> f.do()<br>
> with mock.patch.object(f, 'meth') as meth:<br>
> f.do()<br>
> assert meth.called<br>
> with mock.patch('__main__.Foo.meth') as meth:<br>
> f.do()<br>
> assert meth.called<br>
><br>
> This example is a bit contrived, but should get the point across. I believe<br>
> I understand why the former doesn't work (I need to explicitly call "meth"<br>
> with "self", thus it isn't an instance attribute), but I don't understand<br>
> why the latter doesn't work, either...<br>
</div></div>> _______________________________________________<br>
> testing-in-python mailing list<br>
> <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
> <a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
><br>
><br>
</blockquote></div><br>