Skip to content

Commit 13dd6eb

Browse files
sj26yujinakayama
authored andcommitted
Fix noisy log error (#1831)
* Fix noisy log error We've been seeing this error in our test logs: Could not log "render_template.action_view" event. NoMethodError: undefined method `example_group' for nil:NilClass .../rspec-rails-3.6.0/lib/rspec/rails/view_rendering.rb:67:in `current_example_group' .../rspec-rails-3.6.0/lib/rspec/rails/view_rendering.rb:71:in `render_template' ... This is while running an rspec rails feature spec using capybara, so this is a separate thread running webrick. This was also during a before block. In any case, this should probably fail gracefully when there is no example. While updating this code I got an "ExampleGroup doesn't respond to render_views?", so built in that case too. * Throw away the LogSubscriber and just log explicitly
1 parent 1cc844e commit 13dd6eb

File tree

2 files changed

+2
-30
lines changed

2 files changed

+2
-30
lines changed

lib/rspec/rails/view_rendering.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,6 @@ def self.nullify_template_rendering(templates)
6161
end
6262
end
6363

64-
# @private
65-
class LogSubscriber < ::ActiveSupport::LogSubscriber
66-
def current_example_group
67-
RSpec.current_example.example_group
68-
end
69-
70-
def render_template(_event)
71-
return if current_example_group.render_views?
72-
info(" Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.")
73-
end
74-
end
75-
76-
LogSubscriber.attach_to(:action_view)
77-
7864
# Delegates all methods to the submitted resolver and for all methods
7965
# that return a collection of `ActionView::Template` instances, return
8066
# templates with modified source
@@ -117,6 +103,8 @@ def find_templates(*args)
117103
# @private
118104
class EmptyTemplateHandler
119105
def self.call(_template)
106+
::Rails.logger.info(" Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.")
107+
120108
%("")
121109
end
122110
end

spec/rspec/rails/view_rendering_spec.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ def example.controller
4747
group.render_views true
4848
expect(group.new.render_views?).to be_truthy
4949
end
50-
51-
it "does not log a message that rendering was prevented" do
52-
subscriber = RSpec::Rails::ViewRendering::EmptyTemplateResolver::LogSubscriber.new
53-
allow(subscriber).to receive(:current_example_group).and_return group
54-
expect(subscriber).to_not receive(:info)
55-
group.render_views true
56-
subscriber.render_template(nil)
57-
end
5850
end
5951

6052
context "with false" do
@@ -68,14 +60,6 @@ def example.controller
6860
group.render_views false
6961
expect(group.new.render_views?).to be_falsey
7062
end
71-
72-
it "logs a message that rendering was prevented" do
73-
subscriber = RSpec::Rails::ViewRendering::EmptyTemplateResolver::LogSubscriber.new
74-
allow(subscriber).to receive(:current_example_group).and_return group
75-
expect(subscriber).to receive(:info).with /render_views/
76-
group.render_views false
77-
subscriber.render_template(nil)
78-
end
7963
end
8064

8165
it 'propogates to examples in nested groups properly' do

0 commit comments

Comments
 (0)