【问题标题】:fakeredis between multiple django views多个 django 视图之间的 fakeredis
【发布时间】:2020-09-29 05:15:05
【问题描述】:

我有一个涉及多个 Django 视图的测试
似乎 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 不能在多个视图之间传递,这是我需要的
提前致谢,
纳达夫

【问题讨论】:

    标签: django python-3.x redis fakeredis


    【解决方案1】:

    我的解决方案涉及使用 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

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 2011-12-07
      相关资源
      最近更新 更多