Mocking async call in python 3.5

后端 未结 7 1700
花落未央
花落未央 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:20

    Based on @scolvin answer I created this (imo) cleaner way:

    def async_return(result):
        f = asyncio.Future()
        f.set_result(result)
        return f
    

    That's it, just use it around whatever return you want to be async, as in

    mock = MagicMock(return_value=async_return("Example return"))
    await mock()
    

提交回复
热议问题