Skip to content

Commit ee0cb7d

Browse files
committed
Log a message when view rendering is intercepted
1 parent 3caa42e commit ee0cb7d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/rspec/rails/view_rendering.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ 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+
unless 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+
end
76+
77+
LogSubscriber.attach_to :action_view
78+
6479
# Delegates all methods to the submitted resolver and for all methods
6580
# that return a collection of `ActionView::Template` instances, return
6681
# templates with modified source

spec/rspec/rails/view_rendering_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ 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
5058
end
5159

5260
context "with false" do
@@ -60,6 +68,14 @@ def example.controller
6068
group.render_views false
6169
expect(group.new.render_views?).to be_falsey
6270
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
6379
end
6480

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

0 commit comments

Comments
 (0)