Skip to content

Commit dcc0204

Browse files
committed
Use fakeredis for tests
1 parent 8890743 commit dcc0204

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

Diff for: Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ gem "importmap-rails"
5454
group :test do
5555
gem "simplecov", require: false
5656
gem "simplecov-cobertura"
57+
gem 'fakeredis'
5758
end

Diff for: Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ GEM
102102
factory_bot_rails (6.4.3)
103103
factory_bot (~> 6.4)
104104
railties (>= 5.0.0)
105+
fakeredis (0.1.4)
105106
fugit (1.11.1)
106107
et-orbi (~> 1, >= 1.2.11)
107108
raabro (~> 1.4)
@@ -303,6 +304,7 @@ DEPENDENCIES
303304
debug
304305
dotenv-rails
305306
factory_bot_rails
307+
fakeredis
306308
importmap-rails
307309
puma
308310
rails (~> 7.2.1)

Diff for: config/initializers/redis.rb

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
require "redis"
22

3-
redis_url = ENV["REDIS_URL"] || "redis://localhost:6379/0"
3+
unless defined?(REDIS)
4+
redis_url = ENV["REDIS_URL"] || "redis://localhost:6379/0"
45

5-
REDIS = Redis.new(url: redis_url)
6+
REDIS = Redis.new(url: redis_url)
67

7-
# Add error handling
8-
begin
9-
REDIS.ping
10-
rescue Redis::CannotConnectError => e
11-
Rails.logger.error "Failed to connect to Redis: #{e.message}"
12-
REDIS = nil
8+
# Add error handling
9+
begin
10+
REDIS.ping
11+
rescue Redis::CannotConnectError => e
12+
Rails.logger.error "Failed to connect to Redis: #{e.message}"
13+
REDIS = nil
14+
end
1315
end

Diff for: test/test_helper.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,12 @@ class ActiveSupport::TestCase
2121
# Add more helper methods to be used by all tests here...
2222
end
2323

24-
# Configure Redis for testing
25-
REDIS = Redis.new(url: ENV["REDIS_URL"] || "redis://localhost:6379/1")
24+
# Use FakeRedis for testing if available, otherwise use real Redis
25+
begin
26+
require 'fakeredis/minitest'
27+
puts "Using FakeRedis for testing"
28+
rescue LoadError
29+
puts "FakeRedis not available, using real Redis for testing"
30+
require 'redis'
31+
REDIS = Redis.new(url: ENV['REDIS_URL'] || 'redis://localhost:6379/1')
32+
end

0 commit comments

Comments
 (0)