Skip to content

Commit ab1dbd6

Browse files
committed
try to prevent mock leakage
1 parent 62d3537 commit ab1dbd6

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

spec/logtail-rails/rack_logger_spec.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@ def method_for_action(action_name)
3838
it "should mute the default rails logs" do
3939
allow(::Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production")) # Rails 3.2.X
4040

41-
dispatch_rails_request("/rails_rack_logger")
41+
# Disable Rails 8.1 event logging for this test to avoid extra log lines
42+
original_enabled = Logtail::Integrations::Rails::EventLogSubscriber.enabled
43+
Logtail::Integrations::Rails::EventLogSubscriber.enabled = false
4244

43-
lines = clean_lines(io.string.split("\n"))
44-
expect(lines.length).to eq(3)
45-
expect(lines[0]).to include("Started GET \\\"/rails_rack_logger\\\"")
46-
expect(lines[1]).to include("Processing by RailsRackLoggerController#index as HTML")
47-
expect(lines[2]).to include("Completed 200 OK in 0.0ms")
45+
begin
46+
dispatch_rails_request("/rails_rack_logger")
47+
48+
lines = clean_lines(io.string.split("\n"))
49+
expect(lines.length).to eq(3)
50+
expect(lines[0]).to include("Started GET \\\"/rails_rack_logger\\\"")
51+
expect(lines[1]).to include("Processing by RailsRackLoggerController#index as HTML")
52+
expect(lines[2]).to include("Completed 200 OK in 0.0ms")
53+
ensure
54+
Logtail::Integrations::Rails::EventLogSubscriber.enabled = original_enabled
55+
end
4856
end
4957
end
5058
end

spec/spec_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@
3737
config.expect_with :rspec do |c|
3838
c.syntax = :expect
3939
end
40+
41+
# Reset Logtail Config.instance.logger before each test to prevent mock leakage
42+
config.before(:each) do
43+
# Reset to default Rails logger proc to prevent mock leakage between tests
44+
Logtail::Config.instance.logger = Proc.new { ::Rails.logger }
45+
46+
# Stub EventLogSubscriber#logger to prevent mock leakage from cached instances
47+
allow_any_instance_of(Logtail::Integrations::Rails::EventLogSubscriber).to receive(:logger).and_return(Logtail::Config.instance.logger)
48+
end
4049
end

0 commit comments

Comments
 (0)