kaste / mockito-python

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

"Called but not expected" with mocked open() #75

Closed programmerPhysicist closed 8 months ago

programmerPhysicist commented 8 months ago

When I try to mock open() with mockito in python, I get the following error:

Called but not expected:

    open('/usr/lib/python3.9/__pycache__/doctest.cpython-39.pyc', 'rb')

Stubbed invocations are:

    open('oneWay_matchDict.pkl', 'rb')

The line to be mocked looks like this:

pkl_file = open('oneWay_matchDict.pkl','rb')

While the mock looks like this:

pkl_file = mock()
when2(open, 'oneWay_matchDict.pkl', 'rb').thenReturn(pkl_file)

How do I ignore other invocations of open?

kaste commented 8 months ago

I think you can do this either so

pkl_file = mock()
when2(open, ...).thenCallOriginalImplementation()
when2(open, 'oneWay_matchDict.pkl', 'rb').thenReturn(pkl_file)

or so

pkl_file = mock()
spy2(open)
when2(open, 'oneWay_matchDict.pkl', 'rb').thenReturn(pkl_file)

🤞