bpo-38136: Updates await_count and call_count to be different things (GH-16192)

This commit is contained in:
Lisa Roach 2019-09-23 20:49:40 -07:00 committed by GitHub
parent 6f53d34fb0
commit ef04851775
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 208 additions and 43 deletions

View file

@ -514,21 +514,6 @@ the *new_callable* argument to :func:`patch`.
>>> mock.call_count
2
For :class:`AsyncMock` the :attr:`call_count` is only iterated if the function
has been awaited:
>>> mock = AsyncMock()
>>> mock() # doctest: +SKIP
<coroutine object AsyncMockMixin._mock_call at ...>
>>> mock.call_count
0
>>> async def main():
... await mock()
...
>>> asyncio.run(main())
>>> mock.call_count
1
.. attribute:: return_value
Set this to configure the value returned by calling the mock:
@ -907,19 +892,22 @@ object::
.. method:: assert_awaited()
Assert that the mock was awaited at least once.
Assert that the mock was awaited at least once. Note that this is separate
from the object having been called, the ``await`` keyword must be used:
>>> mock = AsyncMock()
>>> async def main():
... await mock()
>>> async def main(coroutine_mock):
... await coroutine_mock
...
>>> asyncio.run(main())
>>> coroutine_mock = mock()
>>> mock.called
True
>>> mock.assert_awaited()
>>> mock_2 = AsyncMock()
>>> mock_2.assert_awaited()
Traceback (most recent call last):
...
AssertionError: Expected mock to have been awaited.
>>> asyncio.run(main(coroutine_mock))
>>> mock.assert_awaited()
.. method:: assert_awaited_once()
@ -1004,14 +992,15 @@ object::
... await mock(*args, **kwargs)
...
>>> calls = [call("foo"), call("bar")]
>>> mock.assert_has_calls(calls)
>>> mock.assert_has_awaits(calls)
Traceback (most recent call last):
...
AssertionError: Calls not found.
AssertionError: Awaits not found.
Expected: [call('foo'), call('bar')]
Actual: []
>>> asyncio.run(main('foo'))
>>> asyncio.run(main('bar'))
>>> mock.assert_has_calls(calls)
>>> mock.assert_has_awaits(calls)
.. method:: assert_not_awaited()