Spying on `__call__` doesn't work
hsorsky opened this issue · 1 comments
hsorsky commented
Copied from the docs but updated to use __call__
instead of bar
def test_spy_method(mocker):
class Foo(object):
def __call__(self, v):
return v * 2
foo = Foo()
spy = mocker.spy(foo, '__call__')
assert foo(21) == 42
spy.assert_called_once_with(21)
raises an assertion error
E AssertionError: Expected '__call__' to be called once. Called 0 times.
when I'd expect it to pass.
I assume this has something to do with the special nature of __call__
?
nicoddemus commented
What happens (IIRC) is that special methods are called via descriptors and bypass instance lookup; if you install the spy into Foo
(the class, instead of the instance) then it works.
Closing for now because there's nothing that can be done on pytest-mock's side, feel free to follow up with further questions. 👍