The below example class has a property bar
that is awaitable, as in async_main()
because it (theoretically) does some IO work before returning the answer to everything.
class Foo:
@property
async def bar(self):
return 42
async def async_main():
f = Foo()
print(await f.bar)
I'm having trouble testing it, as the usual suspects of Mock, MagicMock, and AsyncMock don't work with properties as expected. My current workaround is:
f.bar = some_awaitable()
since this makes f.bar a 'field' that can be awaited, but unfortunately I need to access it multiple times while it's under test, which yields RuntimeError: cannot reuse already awaited coroutine
on the second access of course.
Is there an established way to mock an async property like this?