Reputation: 26608
eg in t.py
def a(obj):
print obj
def b():
a(1)
a(2)
then:
from t import b
with patch('t.a') as m:
b()
m.assert_called_with(1)
I get:
AssertionError: Expected call: a(1)
Actual call: a(2)
Upvotes: 5
Views: 3178
Reputation: 474251
The most straightforward approach would be to get the first item from mock.call_args_list
and check if it is called with 1
:
call_args_list
This is a list of all the calls made to the mock object in sequence (so the length of the list is the number of times it has been called).
assert m.call_args_list[0] == call(1)
where call
is imported from mock
: from mock import call
.
Also, mock_calls
would work in place of call_args_list
too.
Another option would be to use assert_any_call()
:
m.assert_any_call(1)
Upvotes: 10