kaste / mockito-python

Mockito is a spying framework
MIT License
123 stars 12 forks source link

ArgumentError: obj is not registered #47

Closed ghost closed 2 years ago

ghost commented 2 years ago

My test fails with an unexpected error. As far as I can tell from the documentation, I'm doing this right. Am I missing something?

from mockito import mock, verify

mock_feature_0 = mock()
def test_something():
    verify(mock_feature_0).listen(...)

This test should fail because listen was not invoked. Instead I get this error.

mockito.mockito.ArgumentError: obj '<Dummy id=139889037572992>' is not registered

It seems that the error comes from _get_mock_or_raise in mockito.py. I don't know how to register my mock other than what I'm already doing.

Other Information

$ python3 -m pytest --version
pytest 7.1.2

Test command

python3 -m pytest -v

kaste commented 2 years ago

Hm, I don't think I can reproduce that. I get

|| D:\mockito-issue-47 =
> pytest
================================================= test session starts =================================================
platform win32 -- Python 3.10.0, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\mockito-issue-47
plugins: forked-1.3.0, xdist-2.4.0
collected 1 item

test_issue.py F                                                                                                  [100%]

====================================================== FAILURES =======================================================
___________________________________________________ test_something ____________________________________________________

    def test_something():
>       verify(mock_feature_0).listen(...)
E       mockito.verification.VerificationError:
E       Wanted but not invoked:
E
E           listen(...)
E
E       Instead got:
E
E           Nothing

test_issue.py:5: VerificationError
=============================================== short test summary info ===============================================
FAILED test_issue.py::test_something - mockito.verification.VerificationError:
================================================== 1 failed in 0.10s ==================================================

Since mock_feature_0 = mock() is executed at import time, maybe you unregister before the test, for example using a pytest hook. You should not do create and config a mock on the module level but inside of the test function because you want to clean up either after or before the test functions anyway. mock()s aren't objects (or even singletons) you configure once and then use multiple times. You usually use them once and then garbage collect them as they store information about their usage.

kaste commented 2 years ago

Actually, you may have https://github.com/kaste/pytest-mockito installed which can be used to cleanup between the tests.

ghost commented 2 years ago

Okay, when I define mock_feature_0 inside of the test function, it works. Thank you! I'm still new to Python, so still figuring out the scoping rules.

Now I'm mocking mock_feature_0 in a setup function, which is what I was trying to get at with mocking it at the module level.

kaste commented 2 years ago

Yeah, using a setup function is plausible, typically call unstub() during the teardown function. (For example, just like https://github.com/SublimeLinter/SublimeLinter/blob/master/tests/test_filter_results.py#L42-L43)

Closing for now. Feel free to ask further questions.