Answer the question
In order to leave comments, you need to log in
How to use AsyncMock?
Good afternoon. I have a class for working with a database. I would like to implement tests for it, I want to use AsyncMock and check how it was called with what parameters. I am getting error AttributeError: 'coroutine' object has no attribute 'objects'
class SyncDB(Actor):
category: ACTORS
database: BaseDBQuery
control: t.Any
def __init__(self, category: ACTORS, control: t.Any, database: BaseDBQuery) -> None:
self.category = category
self.database = database()
self.control = control()
super().__init__()
async def __call__(self) -> None:
for model in await self.control.objects(state=True):
if not model.oid:
result = self.database.create(
json=json.dumps(model.as_dict())
)
create_rows_count += result.status
...
@mark.asyncio
async def test_sync_bank(bank):
mock_bank_control = AsyncMock()
mock_bank_control.objects = AsyncMock(return_value=[bank])
mock_bank_db = AsyncMock(BankDB)
actor = SyncDB(ACTORS.SYNC_BANK, control=mock_bank_control, database=mock_bank_db)
await actor.__call__()
Answer the question
In order to leave comments, you need to log in
This error "AttributeError: 'coroutine' object has no attribute 'objects'" indicates that you are trying to get and set the objects attribute for a coroutine object at the same time. This doesn’t work with them) You need to “execute” (that is, apply the await keyword ) a coroutine (coroutine) and get some kind of response already.
Most likely, in your case you need to write something like this:
mock_bank_control = AsyncMock()
objects = await mock_bank_control(return_value=[bank])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question