我最近注意到,如果我使用mock.patch它模拟一个方法,则不会列出在该call_args字段中传递给mocked方法的实例对象.这是设计的吗?下面的代码/输出可能更好地解释我的意思:
#!/usr/bin/env python2
from mock import patch
class Dog(object):
def bark(self):
print("Woof woof!")
Dog().bark()
def new_method(*args):
print("args = %s" % str(args))
Dog.bark = new_method
Dog().bark()
with patch.object(Dog, "bark"):
d = Dog()
d.bark()
print("d.bark was called: %s" % str(d.bark.called))
print("d.bark was called with args/kwargs: %s" % str(d.bark.call_args))
Run Code Online (Sandbox Code Playgroud)
输出是:
Woof woof!
args = (<__main__.Dog object at 0x7f42c2dbc3d0>,)
# Mocking bit
d.bark was called: True
d.bark was called with args/kwargs: ((), {})
Run Code Online (Sandbox Code Playgroud)
您可以看到实例对象new_method在替换时传递给它bark.但你无法call_args在模拟出来的方法中看到它.这不奇怪吗?我正在使用python模拟库的1.01版本.