I have the following (simplified) piece of code:
def get_redis()
return redis_instance
def bar(key, value, redis=get_redis())
redis.set(key, value)
def foo()
bar("key", value)
In my test I want to mock the function get_redis
to return an instance of fakeredis.FakeStrictRedis()
, so I did this
def test_foo(mocker):
mocker.patch("app.main.get_redis", return_value=fakeredis.FakeStrictRedis())
foo()
the mocked function has no effect, the foo
function try to connect to real redis using the get_redis function from main.
If I wrote in this way works
def bar(key, value)
redis=get_redis()
redis.set(key, value)
This works, but I can pass redis as default value. How can I mock?