From: Arne de Laat Date: Thu, 23 Feb 2017 14:57:25 +0000 (+0100) Subject: bpo-28911: Clarify the behaviour of assert_called_once_with. (#251) X-Git-Tag: v3.7.0a1~1297 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=324c5d8ca6ed1c964d3b20e5762139ec65c7827c;p=python bpo-28911: Clarify the behaviour of assert_called_once_with. (#251) --- diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index ca5c2bc787..cbe0c9adb8 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 315b61157b..6989dc792e 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -811,8 +811,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." %