我在修补基类上的classmethod
时遇到了问题.问题是修补基类会在子类上调用类方法.
我希望这个函数的输出结果是["foo", "bar"]
,但是,在调用类方法之前调用mocker.spy(Foo, "wrapper")
,结果是["foo", "foo"]
.
这是怎么回事?
def test_patching_parent_class(mocker):
"""
pip install pytest pytest-mock
"""
calls = []
class Foo:
def func2(self):
calls.append("foo")
@classmethod
def wrapper(cls):
cls().func2()
class Bar(Foo):
def func2(self):
calls.append("bar")
# Remove this line it will produce the expected result
spy = mocker.spy(Foo, "wrapper")
Foo.wrapper()
Bar.wrapper()
assert calls == ["foo", "bar"]