Skip to content

Commit 96ef56e

Browse files
author
Cosima Radu
committed
Refactor Redis mock in tests to use instance_double for improved clarity and accuracy
1 parent 48a5b41 commit 96ef56e

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

spec/cache_store_redis_spec.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4+
require 'redis/multi_connection'
45

56
RSpec.shared_examples '#set' do |method_name|
67
context 'expires_in' do
78
let(:key) { SecureRandom.uuid }
89
let(:value) { 'SomeValue' }
9-
let(:mock_redis) { instance_double('Redis::MultiConnection') }
10+
let(:mock_redis) { instance_double('Redis') }
1011

1112
before do
1213
allow(Redis::MultiConnection).to receive(:new).and_return(mock_redis)
1314
end
1415

1516
it 'will always set a default TTL if one is not provided' do
16-
expect_any_instance_of(mock_redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
17-
expect_any_instance_of(mock_redis).to receive(:expire).with("test:#{key}", 3_600)
17+
expect(mock_redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
18+
expect(mock_redis).to receive(:expire).with("test:#{key}", 3_600)
1819
subject.public_send(method_name, key, value)
1920
end
2021

2122
it 'will always set a default TTL if an invalid one is provided' do
22-
expect_any_instance_of(mock_redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
23-
expect_any_instance_of(mock_redis).to receive(:expire).with("test:#{key}", 3_600)
23+
expect(mock_redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
24+
expect(mock_redis).to receive(:expire).with("test:#{key}", 3_600)
2425
subject.public_send(method_name, key, value, -200)
2526
end
2627

2728
it 'will always set a default TTL if an invalid one is provided' do
28-
expect_any_instance_of(mock_redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
29-
expect_any_instance_of(mock_redis).to receive(:expire).with("test:#{key}", 3_600)
29+
expect(mock_redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
30+
expect(mock_redis).to receive(:expire).with("test:#{key}", 3_600)
3031
subject.public_send(method_name, key, value, 0.456)
3132
end
3233

3334
it 'will always force the TTL to be an integer' do
34-
expect_any_instance_of(mock_redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
35-
expect_any_instance_of(mock_redis).to receive(:expire).with("test:#{key}", 20)
35+
expect(mock_redis).to receive(:set).with("test:#{key}", "\"#{value}\"")
36+
expect(mock_redis).to receive(:expire).with("test:#{key}", 20)
3637
subject.public_send(method_name, key, value, 20.123)
3738
end
3839
end

0 commit comments

Comments
 (0)