title: Python asyncio: how to mock __aiter__() method?
body: <p>I have a code which is listening to messages on WebSocket using <a href="http://aiohttp.readthedocs.org/en/stable/web_reference.html#websocketresponse" rel="noreferrer"><code>aiohttp</code></a>.</p>
  
  <p>It looks like:</p>
  
  <pre><code>async for msg in ws:
      await self._ws_msg_handler.handle_message(ws, msg, _services)
  </code></pre>
  
  <p>Where <code>ws</code> is an instance of <code>aiohttp.web.WebSocketResponse()</code> (<a href="https://github.com/f0t0n/aiojsonrpc/blob/37a1c2561a846efe465fcb919582f1b54199d346/aiojsonrpc/request_handler.py#L113" rel="noreferrer">original code</a>)</p>
  
  <p>In my test I mock <code>WebSocketResponse()</code> and its <code>__aiter__</code> method:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Though when I run the <a href="https://github.com/f0t0n/aiojsonrpc/blob/37a1c2561a846efe465fcb919582f1b54199d346/tests/test_request_handler.py#L251" rel="noreferrer">test</a> it fails with the error message saying: </p>
  
  <blockquote>
    <p>'async for' requires an object with <code>__aiter__</code> method, got MagicMock</p>
  </blockquote>
  
  <pre><code>=================================================================================== FAILURES ===================================================================================
  __________________________________________________________________________ test_rpc_websocket_handler __________________________________________________________________________
  
  MockWebSocketMessageHandler = &lt;MagicMock name='WebSocketMessageHandler' id='140687969989632'&gt;
  rpc_websocket_handler = &lt;aiojsonrpc.request_handler.RpcWebsocketHandler object at 0x7ff47879b0f0&gt;
  
      @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)
  &gt;           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
  </code></pre>
  
  <p>So it behaves like <code>__aiter__()</code> was never mocked.
  How I'm supposed to accomplish correct mocking in this case?</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>For now I've found a <a href="https://github.com/f0t0n/aiojsonrpc/pull/1/commits/deb80fbf0bf6f3687a2569057a5151f086a8b897#diff-5b5b170d5baa5e91e7556ad1d80c836bR113" rel="noreferrer">workaround</a> to make the code testable though I would really appreciate if someone tell me how to deal with the issue described in the original question.</p>