gh-109350: Fix outdated captured output in unittest.mock documentation (#109353)

This commit is contained in:
Egil Martinsson 2023-09-15 20:25:16 +02:00 committed by GitHub
parent 92ed7e4df1
commit 3d881453d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 10 deletions

View File

@ -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
@ -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
@ -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='...'>
@ -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:

View File

@ -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
@ -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
@ -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)
@ -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
@ -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.
@ -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')