If I understand you correctly, you want to check if the given call is in mock_calls
.
The standard way to check if a method has been called, is to use one of the assert_called_xxx
methods on the created instance:
@mock.patch('mymodule.SomeClass')
def test1(mocked):
inst = SomeClass() # inst == mocked.return_value
inst.__str__.assert_not_called()
# use this if you don't have inst
mocked.return_value.__str__assert_not_called()
print(inst)
inst.__str__.assert_called_once()
mocked.return_value.assert_called_once()
mocked.return_value.__str__.assert_called_once()
Note that instead of checking call().__str__
on mocked
, you check __str__
on mocked.return_value
, which is the result of call()
.
If for some reason you need to directly check for the string representation of that call, e.g. call.__str__()
, you have to check the string instead:
assert 'call.__str__()' in [str(c) for c in inst.mock_calls]
Or, if you want to check it on the class:
assert 'call().__str__()' in [str(c) for c in mocked.mock_calls]
EDIT:
Removed not working version, as pointed out by @user2357112supportsMonica, fixed incorrect usage of mocked
.