Skip to content

Commit 502d5c0

Browse files
committed
experiment! Try using tagged logging for real
1 parent a34dea7 commit 502d5c0

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

lib/rspec/rails/adapters.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,31 @@ def assertion_delegator
185185
# @private
186186
module TaggedLoggingAdapter
187187
require 'active_support/testing/tagged_logging'
188-
include ActiveSupport::Testing::TaggedLogging
189188

190-
# Just a stub as TaggedLogging is calling `name`
191-
def name
189+
def before_setup
190+
@__wrapped_tagged_logger = WrappedTaggedLogging.new(@__inspect_output)
191+
@__wrapped_tagged_logger.before_setup
192+
end
193+
194+
def tagged_logger
195+
@__wrapped_tagged_logger.tagged_logger
196+
end
197+
198+
class WrappedTaggedLogging
199+
# TaggedLogging attempts calling `super`
200+
include Module.new { def before_setup; end }
201+
include ActiveSupport::Testing::TaggedLogging
202+
203+
attr_reader :name
204+
205+
def initialize(name)
206+
@name = name
207+
end
208+
209+
# It is private in the TaggedLogging module
210+
def tagged_logger
211+
super
212+
end
192213
end
193214
end
194215
end

snippets/include_activesupport_testing_tagged_logger.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
end
3030

3131
gem "rspec-rails", path: "../"
32+
gem 'pry-byebug'
3233
end
3334

3435
# Run specs at exit
@@ -54,6 +55,14 @@ class TestError < StandardError; end
5455
include ::ActiveJob::TestHelper
5556

5657
describe 'error raised in perform_enqueued_jobs with block' do
58+
class StringIncompatible
59+
def to_s
60+
raise 'to_s was called on `name`'
61+
end
62+
end
63+
64+
let(:name) { StringIncompatible.new }
65+
5766
it 'raises the explicitly thrown error' do
5867
allow_any_instance_of(TestJob).to receive(:perform).and_raise(TestError)
5968

@@ -64,8 +73,13 @@ class TestError < StandardError; end
6473
TestError
6574
end
6675

67-
expect { perform_enqueued_jobs { TestJob.perform_later } }
68-
.to raise_error(expected_error)
76+
# expect {
77+
# Necessary to make sure the tagged logging hook runs
78+
Rails.logger = Logger.new($stderr)
79+
80+
expect { perform_enqueued_jobs { TestJob.perform_later } }
81+
.to raise_error(expected_error)
82+
# }.to output(/\[ActiveJob\] \[TestJob\] \[[^\]]+\] Error performing TestJob/).to_stderr
6983
end
7084
end
7185
end

0 commit comments

Comments
 (0)