bpo-38163: Child mocks detect their type as sync or async (GH-16471)

This commit is contained in:
Lisa Roach 2019-09-29 21:56:47 -07:00 committed by GitHub
parent 5bcc6d89bc
commit 3667e1ee6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 28 deletions

View file

@ -865,7 +865,7 @@ object::
True
The result of ``mock()`` is an async function which will have the outcome
of ``side_effect`` or ``return_value``:
of ``side_effect`` or ``return_value`` after it has been awaited:
- if ``side_effect`` is a function, the async function will return the
result of that function,
@ -890,6 +890,32 @@ object::
>>> mock() # doctest: +SKIP
<coroutine object AsyncMockMixin._mock_call at ...>
Setting the *spec* of a :class:`Mock`, :class:`MagicMock`, or :class:`AsyncMock`
to a class with asynchronous and synchronous functions will automatically
detect the synchronous functions and set them as :class:`MagicMock` (if the
parent mock is :class:`AsyncMock` or :class:`MagicMock`) or :class:`Mock` (if
the parent mock is :class:`Mock`). All asynchronous functions will be
:class:`AsyncMock`.
>>> class ExampleClass:
... def sync_foo():
... pass
... async def async_foo():
... pass
...
>>> a_mock = AsyncMock(ExampleClass)
>>> a_mock.sync_foo
<MagicMock name='mock.sync_foo' id='...'>
>>> a_mock.async_foo
<AsyncMock name='mock.async_foo' id='...'>
>>> mock = Mock(ExampleClass)
>>> mock.sync_foo
<Mock name='mock.sync_foo' id='...'>
>>> mock.async_foo
<AsyncMock name='mock.async_foo' id='...'>
.. method:: assert_awaited()
Assert that the mock was awaited at least once. Note that this is separate