1 回答

TA贡献1873条经验 获得超9个赞
确实,您不能混合使用自动指定和新的可调用项。相反,可以自动指定方法,然后替换side_effect属性,并为其指定一个AsyncMock()实例:
from unittest import mock
def configure_coroutine_mock(mock_function, klass=AsyncMock):
"""Make an autospecced async function return a coroutine mock"""
mock_function.side_effect = AsyncMock()
# mark the side effect as a child of the original mock object
# so transitive access is recorded on the parent mock too. This is
# what .return_value does normally
mock._check_and_set_parent(
mock_function.mock, mock_function.side_effect,
None, '()')
return mock_asy_method.side_effect
with mocker.patch('ExampleClass.asy_method', autospec=True) as mock_asy_method:
configure_coroutine_mock(mock_asy_method)
因为AsyncMock()是可调用的对象,所以每次mock_asy_method调用都会调用,并将参数传递给该对象。然后,该调用的结果用于从返回mock_asy_method():
>>> from unittest import mock
>>> class ExampleClass:
... async def asy_method(self, param):
... return await some_coroutine(self, param)
...
>>> example_instance = ExampleClass()
>>> with mock.patch('__main__.ExampleClass.asy_method', autospec=True) as mock_asy_method:
... configure_coroutine_mock(mock_asy_method)
... print(example_instance.asy_method('foo')) # call to patched class coroutine
... print(mock_asy_method.mock_calls) # calls are recorded
...
<AsyncMock name='asy_method()' id='4563887496'>
<coroutine object AsyncMock.__call__ at 0x1100780f8>
[call(<__main__.ExampleClass object at 0x10ffac1d0>, 'foo')]
如您所见,self参数和参数记录在调用中,因为这mock_asy_method是一种适当的函数。
当然,只有在AsyncMock()实际等待返回的呼叫结果时,我们才会看到该呼叫也被记录:
>>> with mock.patch('__main__.ExampleClass.asy_method', autospec=True) as mock_asy_method:
... configure_coroutine_mock(mock_asy_method)
... loop = asyncio.get_event_loop()
... coro = example_instance.asy_method('foo')
... loop.run_until_complete(coro)
... print(mock_asy_method.mock_calls)
...
<AsyncMock name='asy_method()' id='4564408920'>
<AsyncMock name='asy_method()()' id='4564999360'>
[call(<__main__.ExampleClass object at 0x10ffac1d0>, 'foo'),
call()(<__main__.ExampleClass object at 0x10ffac1d0>, 'foo')]
添加回答
举报