mirror of
https://github.com/python/cpython.git
synced 2025-10-05 06:31:48 +00:00
bpo-28911: Clarify the behaviour of assert_called_once_with. (#254)
(cherry picked from commit 9d56b34af2
)
This commit is contained in:
parent
0246422b97
commit
fa30453568
2 changed files with 8 additions and 7 deletions
|
@ -275,14 +275,14 @@ the *new_callable* argument to :func:`patch`.
|
||||||
|
|
||||||
.. method:: assert_called_once_with(*args, **kwargs)
|
.. method:: assert_called_once_with(*args, **kwargs)
|
||||||
|
|
||||||
Assert that the mock was called exactly once and with the specified
|
Assert that the mock was called exactly once and that that call was
|
||||||
arguments.
|
with the specified arguments.
|
||||||
|
|
||||||
>>> mock = Mock(return_value=None)
|
>>> mock = Mock(return_value=None)
|
||||||
>>> mock('foo', bar='baz')
|
>>> mock('foo', bar='baz')
|
||||||
>>> mock.assert_called_once_with('foo', bar='baz')
|
>>> mock.assert_called_once_with('foo', bar='baz')
|
||||||
>>> mock('foo', bar='baz')
|
>>> mock('other', bar='values')
|
||||||
>>> mock.assert_called_once_with('foo', bar='baz')
|
>>> mock.assert_called_once_with('other', bar='values')
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
AssertionError: Expected 'mock' to be called once. Called 2 times.
|
AssertionError: Expected 'mock' to be called once. Called 2 times.
|
||||||
|
@ -294,7 +294,8 @@ the *new_callable* argument to :func:`patch`.
|
||||||
|
|
||||||
The assert passes if the mock has *ever* been called, unlike
|
The assert passes if the mock has *ever* been called, unlike
|
||||||
:meth:`assert_called_with` and :meth:`assert_called_once_with` that
|
: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 = Mock(return_value=None)
|
||||||
>>> mock(1, 2, arg='thing')
|
>>> mock(1, 2, arg='thing')
|
||||||
|
|
|
@ -795,8 +795,8 @@ class NonCallableMock(Base):
|
||||||
|
|
||||||
|
|
||||||
def assert_called_once_with(_mock_self, *args, **kwargs):
|
def assert_called_once_with(_mock_self, *args, **kwargs):
|
||||||
"""assert that the mock was called exactly once and with the specified
|
"""assert that the mock was called exactly once and that that call was
|
||||||
arguments."""
|
with the specified arguments."""
|
||||||
self = _mock_self
|
self = _mock_self
|
||||||
if not self.call_count == 1:
|
if not self.call_count == 1:
|
||||||
msg = ("Expected '%s' to be called once. Called %s times." %
|
msg = ("Expected '%s' to be called once. Called %s times." %
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue