8
"Eşzamansız" ifadeleri (bu durumda, aioredis'in bağlantı havuzu) kullanan bir yöntem için testler yazmaya çalışıyorum, bağlantıyı redis'le eşleştirmek istiyorum, ancak nasıl olduğunu anlamakta zorlanıyorum."Eşzamansız" ifadeleri nasıl alay edilir?
from asyncio import Future
from unittest.mock import MagicMock
import pytest
# The thing i'm trying to test
async def set_value(redis, value):
# Do things
async with redis.get() as conn:
await conn.set("key", value)
#My Mock classes
class MockRedis():
def get(self):
return MockAsyncPool()
class MockAsyncPool(MagicMock):
async def __aenter__(self):
conn = MagicMock()
f = Future()
f.set_result(True)
conn.set = MagicMock(return_value=f)
return conn
def __aexit__(self, exc_type, exc_val, exc_tb):
pass
# The actual test
@pytest.mark.asyncio
async def test_get_token():
redis = MockRedis()
token = await set_value(redis, 'something')
assert token is not None
Birlikte çalıştırmak: İşte
i bugüne kadar ne varpy.test path/to/file.py
Ve ben bu hatayı alıyorum:
> await conn.set("key", value)
E TypeError: object NoneType can't be used in 'await' expression