Skip to content

Commit aabc27f

Browse files
author
Nicholas Stuart
committed
Move assert_raise into behavior classes
1 parent 934966a commit aabc27f

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

activesupport/test/cache/behaviors/failure_raising_behavior.rb

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,116 @@ module FailureRaisingBehavior
44
def test_fetch_read_failure_raises
55
@cache.write("foo", "bar")
66

7-
emulating_unavailability do |cache|
8-
cache.fetch("foo")
7+
assert_raise Redis::BaseError do
8+
emulating_unavailability do |cache|
9+
cache.fetch("foo")
10+
end
911
end
1012
end
1113

1214
def test_fetch_with_block_read_failure_raises
1315
@cache.write("foo", "bar")
1416

15-
emulating_unavailability do |cache|
16-
cache.fetch("foo") { '1' }
17+
assert_raise Redis::BaseError do
18+
emulating_unavailability do |cache|
19+
cache.fetch("foo") { "1" }
20+
end
1721
end
22+
23+
assert_equal "bar", @cache.read("foo")
1824
end
1925

2026
def test_read_failure_raises
2127
@cache.write("foo", "bar")
2228

23-
emulating_unavailability do |cache|
24-
cache.read("foo")
29+
assert_raise Redis::BaseError do
30+
emulating_unavailability do |cache|
31+
cache.read("foo")
32+
end
2533
end
2634
end
2735

2836
def test_read_multi_failure_raises
2937
@cache.write_multi("foo" => "bar", "baz" => "quux")
3038

31-
emulating_unavailability do |cache|
32-
cache.read_multi("foo", "baz")
39+
assert_raise Redis::BaseError do
40+
emulating_unavailability do |cache|
41+
cache.read_multi("foo", "baz")
42+
end
3343
end
3444
end
3545

3646
def test_write_failure_raises
37-
emulating_unavailability do |cache|
38-
cache.write("foo", "bar")
47+
assert_raise Redis::BaseError do
48+
emulating_unavailability do |cache|
49+
cache.write("foo", "bar")
50+
end
3951
end
4052
end
4153

4254
def test_write_multi_failure_raises
43-
emulating_unavailability do |cache|
44-
cache.write_multi("foo" => "bar", "baz" => "quux")
55+
assert_raise Redis::BaseError do
56+
emulating_unavailability do |cache|
57+
cache.write_multi("foo" => "bar", "baz" => "quux")
58+
end
4559
end
4660
end
4761

4862
def test_fetch_multi_failure_raises
4963
@cache.write_multi("foo" => "bar", "baz" => "quux")
5064

51-
emulating_unavailability do |cache|
52-
cache.fetch_multi("foo", "baz") { |k| "unavailable" }
65+
assert_raise Redis::BaseError do
66+
emulating_unavailability do |cache|
67+
cache.fetch_multi("foo", "baz") { |k| "unavailable" }
68+
end
5369
end
5470
end
5571

5672
def test_delete_failure_raises
5773
@cache.write("foo", "bar")
5874

59-
emulating_unavailability do |cache|
60-
cache.delete("foo")
75+
assert_raise Redis::BaseError do
76+
emulating_unavailability do |cache|
77+
cache.delete("foo")
78+
end
6179
end
6280
end
6381

6482
def test_exist_failure_raises
6583
@cache.write("foo", "bar")
6684

67-
emulating_unavailability do |cache|
68-
cache.exist?("foo")
85+
assert_raise Redis::BaseError do
86+
emulating_unavailability do |cache|
87+
cache.exist?("foo")
88+
end
6989
end
7090
end
7191

7292
def test_increment_failure_raises
7393
@cache.write("foo", 1, raw: true)
7494

75-
emulating_unavailability do |cache|
76-
cache.increment("foo")
95+
assert_raise Redis::BaseError do
96+
emulating_unavailability do |cache|
97+
cache.increment("foo")
98+
end
7799
end
78100
end
79101

80102
def test_decrement_failure_raises
81103
@cache.write("foo", 1, raw: true)
82104

83-
emulating_unavailability do |cache|
84-
cache.decrement("foo")
105+
assert_raise Redis::BaseError do
106+
emulating_unavailability do |cache|
107+
cache.decrement("foo")
108+
end
85109
end
86110
end
87111

88112
def test_clear_failure_returns_nil
89-
emulating_unavailability do |cache|
90-
assert_nil cache.clear
113+
assert_raise Redis::BaseError do
114+
emulating_unavailability do |cache|
115+
cache.clear
116+
end
91117
end
92118
end
93119
end

activesupport/test/cache/stores/redis_cache_store_test.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,8 @@ def emulating_unavailability
291291
old_client = Redis.send(:remove_const, :Client)
292292
Redis.const_set(:Client, UnavailableRedisClient)
293293

294-
assert_raise Redis::BaseConnectionError do
295-
yield ActiveSupport::Cache::RedisCacheStore.new(namespace: @namespace, error_handler: -> (method:, returning:, exception:) { raise exception })
296-
end
294+
yield ActiveSupport::Cache::RedisCacheStore.new(namespace: @namespace,
295+
error_handler: -> (method:, returning:, exception:) { raise exception })
297296
ensure
298297
Redis.send(:remove_const, :Client)
299298
Redis.const_set(:Client, old_client)
@@ -308,9 +307,8 @@ def emulating_unavailability
308307
old_client = Redis.send(:remove_const, :Client)
309308
Redis.const_set(:Client, MaxClientsReachedRedisClient)
310309

311-
assert_raise Redis::CommandError do
312-
yield ActiveSupport::Cache::RedisCacheStore.new(namespace: @namespace, error_handler: -> (method:, returning:, exception:) { raise exception })
313-
end
310+
yield ActiveSupport::Cache::RedisCacheStore.new(namespace: @namespace,
311+
error_handler: -> (method:, returning:, exception:) { raise exception })
314312
ensure
315313
Redis.send(:remove_const, :Client)
316314
Redis.const_set(:Client, old_client)

0 commit comments

Comments
 (0)