我正在尝试使用(backported)mock
模块将间谍附加到类中的方法。也就是说,我想创建一个类似于原始方法的模拟,但提供常见的Mock
功能,如call_count
等。
这是我目前正在使用的代码:
import mock
class MyClass(object):
def my_method(self, arg):
return arg + 1
def unit_under_test():
inst = MyClass()
return inst.my_method(1)
with mock.patch.object(MyClass, 'my_method', autospec=True,
side_effect=MyClass.my_method) as spy:
result = unit_under_test()
assert result == 2
assert spy.call_count == 1
这很好用。现在我想使用MagicMock
的自定义子类。 patch
documentation表示可以通过new_callable
参数完成此操作。但是,new_callable
和autospec
不能一起使用:
class MyMock(mock.MagicMock):
pass
with mock.patch.object(MyClass, 'my_method', autospec=True,
side_effect=MyClass.my_method,
new_callable=MyMock) as spy:
...
Traceback (most recent call last):
File "./mocktest.py", line 19, in <module>
new_callable=MyMock) as spy:
File "/var/foo/venv/local/lib/python2.7/site-packages/mock.py", line 1442, in _patch_object
spec_set, autospec, new_callable, kwargs
File "/var/foo/venv/local/lib/python2.7/site-packages/mock.py", line 1127, in __init__
"Cannot use 'autospec' and 'new_callable' together"
ValueError: Cannot use 'autospec' and 'new_callable' together
问题是,遗漏autospec
也不起作用,因为self
未传递给原始方法:
with mock.patch.object(MyClass, 'my_method',
side_effect=MyClass.my_method,
new_callable=MyMock) as spy:
...
Traceback (most recent call last):
File "./mocktest.py", line 20, in <module>
result = unit_under_test()
File "./mocktest.py", line 11, in unit_under_test
return inst.my_method(1)
File "/var/foo/venv/local/lib/python2.7/site-packages/mock.py", line 955, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/var/foo/venv/local/lib/python2.7/site-packages/mock.py", line 1018, in _mock_call
ret_val = effect(*args, **kwargs)
TypeError: unbound method my_method() must be called with MyClass instance as first argument (got int instance instead)
请注意,我无法修补my_method
实例的MyClass
,因为我正在测试的代码会创建自己的MyClass
实例(如图所示)上面的示例代码。)