Mocking async call in python 3.5

后端 未结 7 1634
花落未央
花落未央 2020-12-04 23:38

How do I mock async call from one native coroutine to other one using unittest.mock.patch?

I currently have quite an awkward solution:

c         


        
7条回答
  •  一生所求
    2020-12-05 00:06

    Everyone's missing what's probably the simplest and clearest solution:

    @patch('some.path')
    def test(self, mock):
        f = asyncio.Future()
        f.set_result('whatever result you want')
        process_smtp_message.return_value = f
        mock.assert_called_with(1, 2, 3)
    

    remember a coroutine can be thought of as just a function which is guaranteed to return a future which can, in turn be awaited.

提交回复
热议问题