Testing¶
It’s really easy to cut the dependency with aiocache functionality:
import asyncio
from asynctest import MagicMock
from aiocache.base import BaseCache
async def async_main():
mocked_cache = MagicMock(spec=BaseCache)
mocked_cache.get.return_value = "world"
print(await mocked_cache.get("hello"))
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(async_main())
Note that we are passing the BaseCache as the spec for the Mock (you need to install asynctest
).
Also, for debuging purposes you can use AIOCACHE_DISABLE = 1 python myscript.py to disable caching.