summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArne de Laat <arne@delaat.net>2017-02-23 15:17:11 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2017-02-23 15:17:11 (GMT)
commit55b82e10dc6b75b30a72fa56beb60eaf54a008d4 (patch)
tree1b79416c273ef5dd2c703fc8bc4cf83c3039f31b
parent3cc5817cfaf5663645f4ee447eaed603d2ad290a (diff)
downloadcpython-55b82e10dc6b75b30a72fa56beb60eaf54a008d4.zip
cpython-55b82e10dc6b75b30a72fa56beb60eaf54a008d4.tar.gz
cpython-55b82e10dc6b75b30a72fa56beb60eaf54a008d4.tar.bz2
bpo-28911: Clarify the behaviour of assert_called_once_with. (#252)
(cherry picked from commit 9d56b34af2efc4e266bf3ae62da5cd2e422a42be)
-rw-r--r--Doc/library/unittest.mock.rst11
-rw-r--r--Lib/unittest/mock.py4
2 files changed, 8 insertions, 7 deletions
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index c6d0ec9..a552cbf 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -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.
@@ -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')
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index b6b3836..5f97728 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -815,8 +815,8 @@ class NonCallableMock(Base):
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." %