Skip to content

[3.6] bpo-28911: Clarify the behaviour of assert_called_once_with. #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Doc/library/unittest.mock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,14 @@ the *new_callable* argument to :func:`patch`.

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

Assert that the mock was called exactly once and with the specified
arguments.
Assert that the mock was called exactly once and that that call was
with the specified arguments.

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

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

>>> mock = Mock(return_value=None)
>>> mock(1, 2, arg='thing')
Expand Down
4 changes: 2 additions & 2 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,8 @@ def _error_message():


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