8
"비동기"문 (이 경우 aioredis의 연결 풀)을 사용하는 메서드에 대한 테스트를 작성하려고하는데 연결을 모방하려고합니다. 그러나 문제를 해결하는 데 어려움이 있습니다."async with"문을 조롱하는 방법?
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
내가 그것을 실행 : 여기
내가 지금까지 가지고 무엇py.test path/to/file.py
그리고 난이 오류 받고 있어요 :
> await conn.set("key", value)
E TypeError: object NoneType can't be used in 'await' expression