derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Patch doesn't work when method to patch called from dict. #125

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Run foo.py you'll find that when call_method_normally patching works but when 
call_method_from_dict the patch doesn't work. Not sure if it's a bug or a 
feature :-)

What version of the product are you using? On what operating system?
mock 0.7.2 osx

Original issue reported on code.google.com by misha...@gmail.com on 10 Nov 2011 at 10:14

Attachments:

GoogleCodeExporter commented 9 years ago
What is happening here (with the case that doesn't work) is that you are 
putting a method object into a dictionary. Then your "call_method_from_dict" 
fetches the object back out of the dictionary and calls it.

patch works by overriding name lookup (with the patch in place looking up the 
method "dummy_method" returns the mock). With "call_method_from_dict" the 
*name* "dummy_method" is never looked up, the object is fetched out of the 
dictionary.

You could use patch.dict to replace the method in the dictionary instead.

Original comment by fuzzyman on 10 Nov 2011 at 10:36