Skip to content

Commit 16a7abe

Browse files
authored
Remove mocks from Rails logger test (#30)
1 parent 41b8e88 commit 16a7abe

File tree

1 file changed

+9
-52
lines changed

1 file changed

+9
-52
lines changed
Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,33 @@
11
# frozen_string_literal: true
22

33
require "test_helper"
4+
require "logger"
45
require "rails"
56

67
class TestSidekiqMemoryLoggerRails < Minitest::Test
7-
def test_rails_is_available_and_responds_to_logger
8-
# Verify Rails constant exists and has logger method
9-
assert defined?(Rails), "Rails should be defined"
10-
assert Rails.respond_to?(:logger), "Rails should respond to logger"
8+
def setup
9+
@original_logger = Rails.logger
10+
end
11+
12+
def teardown
13+
Rails.logger = @original_logger
1114
end
1215

1316
def test_configuration_uses_rails_logger_when_rails_logger_available
14-
# Create a mock Rails with a logger
15-
original_logger_method = Rails.method(:logger) if Rails.respond_to?(:logger)
1617
test_logger = Logger.new(StringIO.new)
18+
Rails.logger = test_logger
1719

18-
Rails.define_singleton_method(:logger) { test_logger }
19-
20-
# Create new configuration
2120
config = Sidekiq::MemoryLogger::Configuration.new
2221

23-
# Should use our test Rails.logger
2422
assert_equal test_logger, config.logger
25-
ensure
26-
# Restore original logger method
27-
if original_logger_method
28-
Rails.define_singleton_method(:logger, &original_logger_method)
29-
end
3023
end
3124

3225
def test_configuration_falls_back_when_rails_logger_nil
33-
# Ensure Rails.logger returns nil (default state)
34-
Rails.define_singleton_method(:logger) { nil }
35-
36-
# Create new configuration
37-
config = Sidekiq::MemoryLogger::Configuration.new
38-
39-
# Should fall back to stdout logger since Rails.logger is nil
40-
assert_instance_of Logger, config.logger
41-
assert_equal $stdout, config.logger.instance_variable_get(:@logdev).dev
42-
end
43-
44-
def test_configuration_falls_back_when_rails_not_available
45-
# Temporarily hide Rails constant
46-
rails_backup = Object.send(:remove_const, :Rails) if defined?(Rails)
47-
48-
# Create new configuration instance
49-
config = Sidekiq::MemoryLogger::Configuration.new
50-
51-
# Should fall back to stdout logger
52-
assert_instance_of Logger, config.logger
53-
assert_equal $stdout, config.logger.instance_variable_get(:@logdev).dev
54-
ensure
55-
# Restore Rails constant
56-
Object.const_set(:Rails, rails_backup) if rails_backup
57-
end
58-
59-
def test_configuration_falls_back_when_rails_logger_unavailable
60-
# Create mock Rails without logger method
61-
rails_backup = Object.send(:remove_const, :Rails) if defined?(Rails)
62-
rails_mock = Class.new
63-
Object.const_set(:Rails, rails_mock)
26+
Rails.logger = nil
6427

65-
# Create new configuration instance
6628
config = Sidekiq::MemoryLogger::Configuration.new
6729

68-
# Should fall back to stdout logger since Rails doesn't respond to logger
6930
assert_instance_of Logger, config.logger
7031
assert_equal $stdout, config.logger.instance_variable_get(:@logdev).dev
71-
ensure
72-
# Restore Rails constant
73-
Object.send(:remove_const, :Rails)
74-
Object.const_set(:Rails, rails_backup) if rails_backup
7532
end
7633
end

0 commit comments

Comments
 (0)