Reputation: 5151
I was wondering if anyone has a clever way of testing behavior after a redis key expires. I am essentially building a small redis backed cache for my application and would like to test what happens after a redis key is set to expire.
I am using rspec as my testing framework. I tried to use Timecop to change the time during testing but realized that it would only effect the testing frame work and not the external redis server.
I can set the TTL to 1 and then use a sleep(1) but I would rather not introduce sleeps into my tests.
Does anyone have a good way of testing this?
Upvotes: 9
Views: 9396
Reputation: 5151
The proper way to fix this for the testing environment is to mock out the redis client and have it return the expected value. I have confidence redis will do the right thing and is implemented correctly so mocking this interaction out is probably better than letting the test actually hit redis.
redis_client.should_receive(:ttl).with(key).then_return(-1)
or just mock out the request for the key
redis_client.should_receive(:get).with(key).then_return(nil)
With the newer RSpecs version,
expect(redis_client).to receive(:get).with(key).and_return(value)
Upvotes: 1
Reputation: 10672
Why not use http://redis.io/commands/expire to expire the key right away?
Upvotes: 10