Skip to content

Commit 55b82e1

Browse files
153957vstinner
authored andcommitted
bpo-28911: Clarify the behaviour of assert_called_once_with. (#252)
(cherry picked from commit 9d56b34)
1 parent 3cc5817 commit 55b82e1

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

Doc/library/unittest.mock.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,14 @@ the *new_callable* argument to :func:`patch`.
303303

304304
.. method:: assert_called_once_with(*args, **kwargs)
305305

306-
Assert that the mock was called exactly once and with the specified
307-
arguments.
306+
Assert that the mock was called exactly once and that that call was
307+
with the specified arguments.
308308

309309
>>> mock = Mock(return_value=None)
310310
>>> mock('foo', bar='baz')
311311
>>> mock.assert_called_once_with('foo', bar='baz')
312-
>>> mock('foo', bar='baz')
313-
>>> mock.assert_called_once_with('foo', bar='baz')
312+
>>> mock('other', bar='values')
313+
>>> mock.assert_called_once_with('other', bar='values')
314314
Traceback (most recent call last):
315315
...
316316
AssertionError: Expected 'mock' to be called once. Called 2 times.
@@ -322,7 +322,8 @@ the *new_callable* argument to :func:`patch`.
322322

323323
The assert passes if the mock has *ever* been called, unlike
324324
:meth:`assert_called_with` and :meth:`assert_called_once_with` that
325-
only pass if the call is the most recent one.
325+
only pass if the call is the most recent one, and in the case of
326+
:meth:`assert_called_once_with` it must also be the only call.
326327

327328
>>> mock = Mock(return_value=None)
328329
>>> mock(1, 2, arg='thing')

Lib/unittest/mock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,8 @@ def _error_message():
815815

816816

817817
def assert_called_once_with(_mock_self, *args, **kwargs):
818-
"""assert that the mock was called exactly once and with the specified
819-
arguments."""
818+
"""assert that the mock was called exactly once and that that call was
819+
with the specified arguments."""
820820
self = _mock_self
821821
if not self.call_count == 1:
822822
msg = ("Expected '%s' to be called once. Called %s times." %

0 commit comments

Comments
 (0)