![](/img/trans.png)
[英]Mocking out two redis hgets with different return values in the same python function
[英]Mocking redis in python
我正在嘗試模擬 redis 以測試我在 Django 中構建的 python 應用程序。 我所有的 redis 實例都來自一個名為 record 的模塊,其中的init .py 文件包含:
redis_client = redis.from_url(os.environ.get("REDIS_URL"))
我試圖用mockredis修補實例,但它不起作用。 這是模式代碼:
class TestReleaseDashboard(TestCase):
def setUp(self):
# patch redis
redis_patcher = patch('record.redis_client', mock_redis_client())
self.redis = redis_patcher.start()
self.addCleanup(redis_patcher.stop)
# add data
self.redis.set('LATEST_UPDATE', 'Fall 2012')
self.redis.set('NEXT_UPDATE', 'Spring 2013')
def test_can_filter_pensions(self):
print(redis_client)
print(self.redis)
這兩個打印語句的結果是:
Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
<mockredis.client.MockRedis object at 0x1132924d0>
我能做些什么來解決這個問題並模擬實例?
看起來您正在使用 mockredispy 並且不再受支持:( https://github.com/locationlabs/mockredis
我會看看 fakeredis: https ://pypi.org/project/fakeredis/
我剛剛遇到了同樣的問題,這就是我嘲笑我的redis
實例的方式。 希望它可以幫助某人。 鑒於原始問題的代碼,請使用:
import fakeredis
@patch("redis_instance", fakeredis.FakeStrictRedis())
def test_can_filter_pensions(self):
print(redis_client)
結果:
FakeStrictRedis<ConnectionPool<FakeConnection<server=<fakeredis._server.FakeServer object at 0x7fe87eb99410>,db=0>>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.