使用mock.patch的autospec选项和自定义Mock子类

时间:2016-04-29 09:39:51

标签: python python-2.7 python-mock

我正在尝试使用(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_callableautospec不能一起使用:

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实例(如图所示)上面的示例代码。)

0 个答案:

没有答案
相关问题