-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not mock rq's redis connection with mockredis #282
Comments
If it helps anything, this is how I mock redis with RQ: import redis
import fakeredis
class VeryFakeStrictRedis(fakeredis.FakeStrictRedis, redis.StrictRedis):
pass It haven't backfired yet :) |
@wuub Might be a difference in how we're trying to use the I still think it would be beneficial to improve rq's mock-ability. For instance, what is the need for In addition, it's also impossible to use |
@robodude666 could you show how you patch |
I struggle with this as well. Bummer. |
For my simple use case – avoiding the need for a running Redis server during tests – without any mock call verifications, this seems to work (Python 3.4.2, redis 2.10.3, rq 0.5.2): from unittest.mock import MagicMock
from redis import StrictRedis
class StrictRedisMock(MagicMock, StrictRedis):
@classmethod
def from_url(cls, arg):
return cls() The code under test creates Apply like this: class SomeTest(SomeTestBase):
@patch('redis.StrictRedis.from_url', StrictRedisMock.from_url)
def setUp(self):
super().setUp()
# … HTH |
For the record, I switched from the mock class using diamond inheritance to this: def strict_redis_client_from_url(url):
mock = MagicMock()
mock.__class__ = StrictRedis
return mock Usage: class SomeTest(SomeTestBase):
@patch('redis.StrictRedis.from_url', strict_redis_client_from_url)
def setUp(self):
super().setUp()
# … That said, my two suggested approaches might have serious limitations I haven't (yet) hit. |
I wish there was a better way for me to check this in |
Because of check if connection is instance of
StrictRedis
inrq.compat.connection.patch_connection()
. Is this check really necessary?The text was updated successfully, but these errors were encountered: