Skip to content
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
15 changes: 9 additions & 6 deletions Doc/library/unittest.mock-examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ instantiate the class in those tests.
>>> mock.old_method()
Traceback (most recent call last):
...
AttributeError: object has no attribute 'old_method'
AttributeError: Mock object has no attribute 'old_method'. Did you mean: 'class_method'?

Using a specification also enables a smarter matching of calls made to the
mock, regardless of whether some parameters were passed as positional or
Expand Down Expand Up @@ -798,7 +798,8 @@ If your mock is only being called once you can use the
>>> mock.foo_bar.assert_called_once_with('baz', spam='eggs')
Traceback (most recent call last):
...
AssertionError: Expected to be called once. Called 2 times.
AssertionError: Expected 'foo_bar' to be called once. Called 2 times.
Calls: [call('baz', spam='eggs'), call()].

Both ``assert_called_with`` and ``assert_called_once_with`` make assertions about
the *most recent* call. If your mock is going to be called several times, and
Expand Down Expand Up @@ -927,8 +928,9 @@ Here's an example implementation:
>>> c.assert_called_with(arg)
Traceback (most recent call last):
...
AssertionError: Expected call: mock({1})
Actual call: mock(set())
AssertionError: expected call not found.
Expected: mock({1})
Actual: mock(set())
>>> c.foo
<CopyingMock name='mock.foo' id='...'>

Expand Down Expand Up @@ -1292,8 +1294,9 @@ sufficient:
>>> mock.assert_called_with(Foo(1, 2))
Traceback (most recent call last):
...
AssertionError: Expected: call(<__main__.Foo object at 0x...>)
Actual call: call(<__main__.Foo object at 0x...>)
AssertionError: expected call not found.
Expected: mock(<__main__.Foo object at 0x...>)
Actual: mock(<__main__.Foo object at 0x...>)

A comparison function for our ``Foo`` class might look something like this:

Expand Down
10 changes: 6 additions & 4 deletions Doc/library/unittest.mock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ code if they are used incorrectly:
>>> mock_function('wrong arguments')
Traceback (most recent call last):
...
TypeError: <lambda>() takes exactly 3 arguments (1 given)
TypeError: missing a required argument: 'b'

:func:`create_autospec` can also be used on classes, where it copies the signature of
the ``__init__`` method, and on callable objects where it copies the signature of
Expand Down Expand Up @@ -315,6 +315,7 @@ the *new_callable* argument to :func:`patch`.
Traceback (most recent call last):
...
AssertionError: Expected 'method' to have been called once. Called 2 times.
Calls: [call(), call()].

.. versionadded:: 3.6

Expand Down Expand Up @@ -342,7 +343,7 @@ the *new_callable* argument to :func:`patch`.
Traceback (most recent call last):
...
AssertionError: Expected 'mock' to be called once. Called 2 times.

Calls: [call('foo', bar='baz'), call('other', bar='values')].

.. method:: assert_any_call(*args, **kwargs)

Expand Down Expand Up @@ -392,6 +393,7 @@ the *new_callable* argument to :func:`patch`.
Traceback (most recent call last):
...
AssertionError: Expected 'hello' to not have been called. Called 1 times.
Calls: [call()].

.. versionadded:: 3.5

Expand Down Expand Up @@ -954,7 +956,7 @@ object::
>>> asyncio.run(main())
>>> mock.assert_awaited_once()
>>> asyncio.run(main())
>>> mock.method.assert_awaited_once()
>>> mock.assert_awaited_once()
Traceback (most recent call last):
...
AssertionError: Expected mock to have been awaited once. Awaited 2 times.
Expand All @@ -972,7 +974,7 @@ object::
>>> mock.assert_awaited_with('other')
Traceback (most recent call last):
...
AssertionError: expected call not found.
AssertionError: expected await not found.
Expected: mock('other')
Actual: mock('foo', bar='bar')

Expand Down