I want to assert that one classmethod in a Python class calls another classmethod with a certain set of arguments. I would like the mocked classmethod to be "spec-ed", so it detects if it is called with the wrong number of arguments.
When I patch the classmethod using patch.object(.., autospec=True, ..)
, the classmethod is replaced with a NonCallableMagicMock
and raises an error when I try to call it.
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__)
produces the output:
MagicMock
NonCallableMagicMock
How can I get a spec-ed mock for _internal_classmethod
when the class it belongs to is not mocked?