我有一个使用aiohttp
在WebSocket上监听消息的代码。
看起来像:
async for msg in ws:
await self._ws_msg_handler.handle_message(ws, msg, _services)
其中ws
是aiohttp.web.WebSocketResponse()
(original code)
在我的测试中,我模拟了WebSocketResponse()
及其__aiter__
方法:
def coro_mock(**kwargs):
return asyncio.coroutine(mock.Mock(**kwargs))
@pytest.mark.asyncio
@mock.patch('aiojsonrpc.request_handler.WebSocketMessageHandler')
async def test_rpc_websocket_handler(
MockWebSocketMessageHandler,
rpc_websocket_handler
):
ws_response = 'aiojsonrpc.request_handler.WebSocketResponse'
with mock.patch(ws_response) as MockWebSocketResponse:
MockRequest = mock.MagicMock()
req = MockRequest()
ws_instance = MockWebSocketResponse.return_value
ws_instance.prepare = coro_mock()
ws_instance.__aiter__ = coro_mock(return_value=iter(range(5)))
ws_instance.__anext__ = coro_mock()
handle_msg_result = 'Message processed'
MockWebSocketMessageHandler.handle_message.side_effect = Exception(
handle_msg_result)
msg_handler = MockWebSocketMessageHandler()
with pytest.raises(Exception) as e:
await request_handler.RpcWebsocketHandler(msg_handler)(req)
assert str(e.value) == handle_msg_result
虽然当我运行test时它失败并显示错误消息:
' async for'需要一个
__aiter__
方法的对象,得到MagicMock
=================================================================================== FAILURES ===================================================================================
__________________________________________________________________________ test_rpc_websocket_handler __________________________________________________________________________
MockWebSocketMessageHandler = <MagicMock name='WebSocketMessageHandler' id='140687969989632'>
rpc_websocket_handler = <aiojsonrpc.request_handler.RpcWebsocketHandler object at 0x7ff47879b0f0>
@pytest.mark.asyncio
@mock.patch('aiojsonrpc.request_handler.WebSocketMessageHandler')
async def test_rpc_websocket_handler(
MockWebSocketMessageHandler,
rpc_websocket_handler
):
ws_response = 'aiojsonrpc.request_handler.WebSocketResponse'
with mock.patch(ws_response) as MockWebSocketResponse:
# MockRequest = mock.create_autospec(aiohttp.web_reqrep.Request)
# req = MockRequest(*[None] * 6)
MockRequest = mock.MagicMock()
req = MockRequest()
ws_instance = MockWebSocketResponse.return_value
ret = mock.Mock()
ws_instance.prepare = coro_mock()
ws_instance.__aiter__ = coro_mock(return_value=iter(range(5)))
ws_instance.__anext__ = coro_mock()
handle_msg_result = 'Message processed'
MockWebSocketMessageHandler.handle_message.side_effect = Exception(
handle_msg_result)
msg_handler = MockWebSocketMessageHandler()
with pytest.raises(Exception) as e:
await request_handler.RpcWebsocketHandler(msg_handler)(req)
> assert str(e.value) == handle_msg_result
E assert "'async for' ...got MagicMock" == 'Message processed'
E - 'async for' requires an object with __aiter__ method, got MagicMock
E + Message processed
tests/test_request_handler.py:252: AssertionError
所以它的行为就像__aiter__()
从未被嘲笑过。
在这种情况下,我应该如何完成正确的嘲弄?
更新
目前我已找到一个workaround来使代码可以测试,但如果有人告诉我如何处理原始问题中描述的问题,我真的很感激。
答案 0 :(得分:4)
您可以使模拟类返回实现预期接口的对象:
class AsyncIterator:
def __init__(self, seq):
self.iter = iter(seq)
async def __aiter__(self):
return self
async def __anext__(self):
try:
return next(self.iter)
except StopIteration:
raise StopAsyncIteration
MockWebSocketResponse.return_value = AsyncIterator(range(5))
我认为还没有一种方法可以正确地模拟实现__aiter__
的对象,它可能是一个python错误,因为async for
拒绝MagicMock
,即使hasattr(the_magic_mock, '__aiter__')
为True
。
EDIT(13/12/2017):库异步测试支持异步迭代器和上下文管理器,因为0.11,asynctest.MagicMock免费提供此功能。
答案 1 :(得分:1)
为了后代,我遇到了需要测试async for
循环的问题,但是公认的解决方案似乎不适用于Python 3.7。以下示例适用于3.6.x
和3.7.0
,但不适用于{>的{em} :
3.5.x
使用上面的方法,模拟它类似于:
import asyncio
class AsyncIter:
def __init__(self, items):
self.items = items
async def __aiter__(self):
for item in self.items:
yield item
async def print_iter(items):
async for item in items:
print(item)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
things = AsyncIter([1, 2, 3])
loop.run_until_complete(print_iter(things))
loop.close()
答案 2 :(得分:1)
适用于py38
Range("A1").FormulaLocal = "=Somme(A2;A5)"