Skip to content

bpo-28911: Clarify the behaviour of assert_called_once_with. #251

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"that that"? "that the call"? "that call"?

Sorry, english is not my first language, so I don't know if it's correct or not!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that it does look a bit weird at first, but it is acceptable English: http://english.stackexchange.com/questions/3418/how-do-you-handle-that-that-the-double-that-problem

It could be rephrased, but that would make it longer/more complex.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's a little awkward but totally fine.

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 @@ -811,8 +811,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