使用Python 2.7和mock库
我如何使用mock来测试已经被patch的对象是否初始化了特定的参数?
这里是一些示例代码和伪代码:
unittest.py:
import mock
@mock.patch('mylib.SomeObject')
def test_mytest(self, mock_someobject):
test1 = mock_someobject.return_value
test1 = method_inside_someobject.side_effect = ['something']
mylib.method_to_test()
# How can I assert that method_to_test instanced SomeObject with certain arguments?
# I further test things with that method_inside_someobject call, no problems there...
mylib.py:
from someobjectmodule import SomeObject
def method_to_test():
obj = SomeObject(arg1=val1, arg2=val2, arg3=val3)
obj.method_inside_someobject()
那么,我该如何测试SomeObject是使用arg1=val1、arg2=val2、arg3=val3实例化的?
assert_called_with
吗? - mgilsonmethod_inside_someobject
是什么?你是不是想在那里使用test1
?为什么要重新绑定test1
? - Martijn PietersSomeObject
的方式是正确的,这意味着您一定会看到SomeObject.assert_called_with()
起作用。您能否确保您的 [mcve] 正确反映了您的情况,并且可以正常工作? - Martijn Pieters