lundi 2 avril 2018

How to mock redis for Django tests

I am trying to mock out redis in my Django application. I have tried several different methods but none seem to work. What am I doing wrong?

My primary redis instance is called with:

redis_client = redis.from_url(os.environ.get("REDIS_URL"))

That instance is imported in other parts of the app in order to add and retrieve data.

In my tests I tried doing:

import fakeredis
from mock import patch

class TestViews(TestCase):
    def setUp(self):
        redis_patcher = patch('redis.Redis', fakeredis.FakeRedis)
        self.redis = redis_patcher.start()

        self.redis.set('UPDATE', 'Spring')
        print(redis_client.get('UPDATE'))

    def tearDown(self):
        self.redis_patcher.stop

When running the tests I want the 'UPDATE' variable to be set. But instead every instance of redis_client fails saying the server is not available. How can I mock out redis and set values, so that they are available when testing my app?

Aucun commentaire:

Enregistrer un commentaire