django - 多个Django View 之间的fakeredis

标签 django python-3.x redis fakeredis

我有一个涉及多个Django View 的测试

似乎不是在多个 View 之间共享fakeredis
我尝试运行以下代码:

import fakeredis
from testfixtures import Replacer


class TestWithFakeRedis(TestCase):
    def setup_redis(self, test_func):
        fake_redis = fakeredis.FakeStrictRedis()
        with Replacer() as replace:
            replace('app1.views.redis_connection', fake_redis)
            replace("app2.views.redis_connection", fake_redis)
            replace("app2.views.redis_connection", fake_redis)
            test_func(fake_redis)

    def test_something(self):
         def test_func(redis_connection):
            # some testing coded here
            pass
         self.setup_redis(test_func)

fakeredis无法在多个 View 之间传递,这是我需要的东西

提前致谢,

纳达夫

最佳答案

我的解决方案涉及使用unittest.mock.patch:

import fakeredis
fake_redis = fakeredis.FakeRedis()

@patch("app_name1.views.redis_connection", fake_redis)
@patch("app_name2.views.redis_connection", fake_redis)
@patch("app_name3.views.redis_connection", fake_redis)
class TestSomethingWithRedis(TestCase):
    pass

如果您想在测试中检查查询
使用fake_redis

关于django - 多个Django View 之间的fakeredis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62292568/

相关文章:

ruby - 我如何在 Ruby 的陷阱上下文中使用 Redis 调用?

json - Go 语言 Redis : Map & Slice

redis - Redis中键过期后更新值

python - 当我想将数据库从 sqlite 更改为 postgres 时,python 代码有很大变化吗?

Django 在应用程序中找不到模板

python-3.x - Fresh Anaconda 安装从 "ImportError: cannot import name ' 给出 'zmq.backend.cython' 常量“

python - 'numpy.ndarray' object is not callable error with optimize.minimize

python - 在urls.py中传递 'redirect_field_name'不生效

django - 在模板中显示上传的图像 - Django

python - 垂直打印列表字典