[英]How do I add an attribute to AsyncMock?
Code:代码:
class SomeClass(BaseClass):
async def async_method(arg1, arg2, **kwargs):
await self.foo.bar(arg1=arg1, arg2=arg2).baz(**kwargs)
One of the tests used:使用的测试之一:
@pytest.fixture
def same_class():
return SameClass()
async def test_async_method(some_class: SomeClass):
arg1_mock = 1
arg2_mock = 2
some_class.foo.bar = AsyncMock()
some_class.foo.bar.baz = AsyncMock()
await some_class.async_method(arg1=arg1_mock, arg2=arg2_moc)
some_class.foo.bar.assert_called_once()
I get an error:我收到一个错误:
AttributeError: 'coroutine' object has no attribute 'baz'
How to add an asynchronous attribute method to AsyncMock?如何向 AsyncMock 添加异步属性方法? Any help
任何帮助
From the AsyncMock
documentation来自
AsyncMock
文档
The
AsyncMock
object will behave so the object is recognized as an async function, and the result of a call is an awaitable.AsyncMock
object 将运行,因此 object 被识别为异步 function,并且调用的结果是可等待的。
In your code, bar
is the async function that returns an awaitable.在您的代码中,
bar
是异步 function ,它返回一个等待。 To make the awaitable return something with a baz
attribute, set it on the return value instead.要使可等待返回具有
baz
属性的内容,请将其设置为返回值。
some_class.foo.bar = AsyncMock()
some_class.foo.bar.return_value.baz = AsyncMock()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.