Python - object MagicMock can't be used in 'await' expression

前端 未结 5 1266
伪装坚强ぢ
伪装坚强ぢ 2021-02-18 19:49

When I was trying to mock an async function in unittest with MagicMock, I got this exception:

TypeError: object MagicMock can\'t be used in \'await\' expr

5条回答
  •  不要未来只要你来
    2021-02-18 20:32

    I ended up with this hack.

    # monkey patch MagicMock
    async def async_magic():
        pass
    
    MagicMock.__await__ = lambda x: async_magic().__await__()
    

    It only works for MagicMock, not other pre-defined return_value

提交回复
热议问题