Skip to content

Commit bfa6c3f

Browse files
committed
Fix undefined method `example_group' for nil & NoMethodError: render_views? for feature specs
1 parent 375240d commit bfa6c3f

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/rspec/rails/view_rendering.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ def self.nullify_template_rendering(templates)
6464
# @private
6565
class LogSubscriber < ::ActiveSupport::LogSubscriber
6666
def current_example_group
67-
RSpec.current_example.example_group
67+
# When running feature specs current_example can be nil if the subscriber runs in a different context
68+
# than the specs themselves. E.g. in a capybara thread.
69+
RSpec.current_example.try!(:example_group)
6870
end
6971

7072
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+
example_group = current_example_group
74+
if example_group.respond_to?(:render_views?) && !example_group.render_views?
75+
info(" Template rendering was prevented by rspec-rails. Use `render_views` to verify rendered view contents if necessary.")
76+
end
7377
end
7478
end
7579

spec/rspec/rails/view_rendering_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def logger
183183
ActiveSupport::LogSubscriber.logger
184184
end
185185

186-
pending 'does not cause an error' do
186+
it 'does not cause an error' do
187187
expect(logger).not_to receive(:error).with(a_string_starting_with('Could not log "render_template.action_view" event.'))
188188
view.render(body: 'foo')
189189
end

0 commit comments

Comments
 (0)