-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explain how to properly mock user-defined __call__
#131383
Comments
AFAICT, |
Indeed, that's something I missed while browsing the doc. Considering this I would understand if this is not considered as a bug. Either way, I also have a workaround: from unittest.mock import MagicMock
class A:
def __init__(self, a):
...
def __call__(self, b, c):
...
mocker = MagicMock(spec=A, side_effect=MagicMock())
mocker(21, 42)
mocker.side_effect.assert_called_once_with(21, 42) It's not that pretty but does the trick if you really have to mock a callable instance |
@cjw296 do you think it's a legitimate feature request or would it be impossible to make it work properly?^ |
Evidently something is not right here :-) I've only scanned this quickly, but:
What happens if you do: mocker = MagicMock(spec=A(1))` ? |
Then the test passes and |
Okay, so if anything this feels like a documentation issue. Maybe you could work up a documentation PR with changes that would have helped you here? |
Also:
This is the important exception. Why is your test going on to do |
__call__
I completely understand that this could be a documentation issue, especially since in the case of
I guess that's the part that got me confused in the first place, it only raises this exception when calling |
Could you submit a minimal reproducer for this as a test case in a PR to this repo? |
Bug report
Bug description:
Reproduce
Current Behavior
Expected behaviour
It succeeds
Investigation
When passing a class that implements the
__call__
method as spec to a mock instance it takes the it takes the signature of the__init__
which if it is the intended behavior is quite confusing.CPython versions tested on:
3.12, 3.13
Operating systems tested on:
macOS, Linux
The text was updated successfully, but these errors were encountered: