Skip to content

Commit a8be0c3

Browse files
committed
fixed spec/rails_structured_logging/multi_error_reporter_spec.rb
1 parent f7483dd commit a8be0c3

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

lib/rails_structured_logging/multi_error_reporter.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def initialize_reporter
4343
# @return [void]
4444
sig { params(exception: Exception, context: T::Hash[T.untyped, T.untyped]).void }
4545
def report_exception(exception, context = {})
46+
# We still keep this check for backward compatibility with existing code
47+
# but the type signature ensures that exception should never be nil
4648
return if exception.nil?
4749

4850
# Initialize the reporter if it hasn't been initialized yet

spec/rails_structured_logging/multi_error_reporter_spec.rb

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@
66
RSpec.describe RailsStructuredLogging::MultiErrorReporter do
77
let(:exception) { StandardError.new("Test error") }
88
let(:context) { { user_id: 123, action: "test" } }
9+
# Default to no reporter
10+
let(:reporter) { nil }
911

1012
describe '.report_exception' do
1113
before do
1214
# Reset the error reporter before each test
1315
described_class.instance_variable_set(:@error_reporter, nil)
16+
17+
# Hide all error reporting services by default
18+
hide_const("Sentry") if reporter != :sentry && reporter != :all
19+
hide_const("Bugsnag") if reporter != :bugsnag && reporter != :all
20+
hide_const("Rollbar") if reporter != :rollbar && reporter != :all
21+
hide_const("Honeybadger") if reporter != :honeybadger && reporter != :all
22+
1423
# Stub stdout to capture output
1524
allow($stdout).to receive(:puts)
1625
end
1726

1827
context 'when Sentry is available' do
28+
let(:reporter) { :sentry }
29+
1930
before do
20-
stub_const('Sentry', double)
2131
allow(Sentry).to receive(:capture_exception)
2232
end
2333

@@ -44,10 +54,10 @@
4454
end
4555

4656
context 'when Bugsnag is available' do
47-
let(:report) { double }
57+
let(:reporter) { :bugsnag }
58+
let(:report) { double('report') }
4859

4960
before do
50-
stub_const('Bugsnag', double)
5161
allow(Bugsnag).to receive(:notify).and_yield(report)
5262
allow(report).to receive(:add_metadata)
5363
end
@@ -65,8 +75,9 @@
6575
end
6676

6777
context 'when Rollbar is available' do
78+
let(:reporter) { :rollbar }
79+
6880
before do
69-
stub_const('Rollbar', double)
7081
allow(Rollbar).to receive(:error)
7182
end
7283

@@ -82,8 +93,9 @@
8293
end
8394

8495
context 'when Honeybadger is available' do
96+
let(:reporter) { :honeybadger }
97+
8598
before do
86-
stub_const('Honeybadger', double)
8799
allow(Honeybadger).to receive(:notify)
88100
end
89101

@@ -99,6 +111,8 @@
99111
end
100112

101113
context 'when no error reporting service is available' do
114+
# reporter is nil by default
115+
102116
it 'logs to stdout with structured JSON' do
103117
json_output = nil
104118
expect($stdout).to receive(:puts) do |output|
@@ -121,22 +135,10 @@
121135
end
122136
end
123137

124-
context 'when exception is nil' do
125-
it 'does nothing' do
126-
expect(Sentry).not_to receive(:capture_exception) if defined?(Sentry)
127-
expect($stdout).not_to receive(:puts)
128-
described_class.report_exception(nil)
129-
end
130-
end
131-
132138
context 'with multiple error services available' do
133-
before do
134-
# Define all services but only Sentry should be used (first one)
135-
stub_const('Sentry', double)
136-
stub_const('Bugsnag', double)
137-
stub_const('Rollbar', double)
138-
stub_const('Honeybadger', double)
139+
let(:reporter) { :all }
139140

141+
before do
140142
allow(Sentry).to receive(:capture_exception)
141143
allow(Bugsnag).to receive(:notify)
142144
allow(Rollbar).to receive(:error)

0 commit comments

Comments
 (0)