summaryrefslogtreecommitdiffstats
path: root/Doc/library/unittest.mock.rst
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2013-02-02 23:23:58 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2013-02-02 23:23:58 (GMT)
commit5c64df70b5b5cf8df2a651dc6630ab28e30499b8 (patch)
tree8bdb8add55c63239f018a0cc2195c8b619bf0a76 /Doc/library/unittest.mock.rst
parent18b30ee88e47ef85ccc966a178bfd8f64a7f0954 (diff)
downloadcpython-5c64df70b5b5cf8df2a651dc6630ab28e30499b8.zip
cpython-5c64df70b5b5cf8df2a651dc6630ab28e30499b8.tar.gz
cpython-5c64df70b5b5cf8df2a651dc6630ab28e30499b8.tar.bz2
Issue #17015: When it has a spec, a Mock object now inspects its signature when matching calls, so that arguments can be matched positionally or by name.
Diffstat (limited to 'Doc/library/unittest.mock.rst')
-rw-r--r--Doc/library/unittest.mock.rst22
1 files changed, 21 insertions, 1 deletions
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index 8e72696..0df60ca 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -264,7 +264,6 @@ the `new_callable` argument to `patch`.
<Mock name='mock.method()' id='...'>
>>> mock.method.assert_called_with(1, 2, 3, test='wow')
-
.. method:: assert_called_once_with(*args, **kwargs)
Assert that the mock was called exactly once and with the specified
@@ -685,6 +684,27 @@ have to create a dictionary and unpack it using `**`:
...
KeyError
+A callable mock which was created with a *spec* (or a *spec_set*) will
+introspect the specification object's signature when matching calls to
+the mock. Therefore, it can match the actual call's arguments regardless
+of whether they were passed positionally or by name::
+
+ >>> def f(a, b, c): pass
+ ...
+ >>> mock = Mock(spec=f)
+ >>> mock(1, 2, c=3)
+ <Mock name='mock()' id='140161580456576'>
+ >>> mock.assert_called_with(1, 2, 3)
+ >>> mock.assert_called_with(a=1, b=2, c=3)
+
+This applies to :meth:`~Mock.assert_called_with`,
+:meth:`~Mock.assert_called_once_with`, :meth:`~Mock.assert_has_calls` and
+:meth:`~Mock.assert_any_call`. When :ref:`auto-speccing`, it will also
+apply to method calls on the mock object.
+
+ .. versionchanged:: 3.4
+ Added signature introspection on specced and autospecced mock objects.
+
.. class:: PropertyMock(*args, **kwargs)