Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:

assert_called_with only checks against the last call. The docs were have been 
fixed with issue35946. So after multiple calls the you might get an assertion 
error in your proposed implementation leading to the conclusion that the call 
never happened but it's just that the last call doesn't match as explained 
below. mock calls are recorded in mock_calls attribute so you can do a contains 
check by constructing the call object to see if the calls are present. For 
multiple calls you can do a list comprehension and use all().

If it's added it would also mean the addition of assert_not_awaited_with for 
AsyncMock. Given that there is contains check to do it one line I am not sure 
it's worthy enough to expand the API. I couldn't find any related proposals in 
the past. assert_not_called is also a shortcut as added in issue21262. I would 
wait for others opinion on this.

./python
Python 3.9.0a0 (heads/master:392a13bb93, Oct 16 2019, 22:59:54) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest.mock import Mock, call
>>> m = Mock()
>>> m(1)
<Mock name='mock()' id='140089503302176'>
>>> m(2)
<Mock name='mock()' id='140089503302176'>
>>> m.assert_called_with(1) # Raises AssertionError but the call was actually 
>>> made.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xtreak/stuff/python/cpython/Lib/unittest/mock.py", line 902, in 
assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: expected call not found.
Expected: mock(1)
Actual: mock(2)
>>> call(3) not in m.mock_calls # Use contains check instead of iterating 
>>> through call_args_list
True
>>> call(1) not in m.mock_calls
False

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38494>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to