bpo-38136: Updates await_count and call_count to be different things (GH-16192)
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index b446ddb..300f28c 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -514,21 +514,6 @@
>>> 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 @@
.. 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 @@
... 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()