如何在unmocked类中使用autospec修补classmethod?

时间:2014-08-30 23:03:21

标签: python mocking static-methods class-method python-mock

我想声明Python类中的一个类方法调用另一个带有一组参数的classmethod。我希望模拟的classmethod是“spec-ed”,因此它会检测是否使用错误的参数调用它。

当我使用patch.object(.., autospec=True, ..)修补classmethod时,classmethod将替换为NonCallableMagicMock,并在我尝试调用时引发错误。

from mock import patch

class A(object):

    @classmethod
    def api_meth(cls):
        return cls._internal_classmethod(1, 2, 3)

    @classmethod
    def _internal_classmethod(cls, n, m, o):
        return sum(n, m, o)

with patch.object(A, '_internal_classmethod') as p:
    print(type(p).__name__)

with patch.object(A, '_internal_classmethod', autospec=True) as p:
    print(type(p).__name__)

产生输出:

MagicMock
NonCallableMagicMock

_internal_classmethod所属的类没有被模拟时,如何获得{{1}}的规范模拟?

2 个答案:

答案 0 :(得分:7)

有一个未解决的错误报告(google code linkpython bug tracker link)来解决此问题。在修复程序合并之前,您可以尝试以下操作,这对我有用[在2.7,但我认为它也适用于3.x]。

def _patched_callable(obj):
    "Monkeypatch to allow autospec'ed classmethods and staticmethods."
    # See https://code.google.com/p/mock/issues/detail?id=241 and
    # http://bugs.python.org/issue23078 for the relevant bugs this
    # monkeypatch fixes
    if isinstance(obj, type):
        return True
    if getattr(obj, '__call__', None) is not None:
        return True
    if (isinstance(obj, (staticmethod, classmethod))
        and mock._callable(obj.__func__)):
        return True
    return False
_patched_callable._old_func = mock._callable
mock._callable = _patched_callable

在monkeypatch之后,您应该能够正常使用mock.patch并正确修补静态和类方法。

答案 1 :(得分:5)

使用spec代替autospec,然后直接进行设置。

with patch.object(A, '_internal_classmethod', spec=A._internal_classmethod) as p:
    print(type(p).__name__)

给了我

MagicMock
输出