Skip to content

Commit

Permalink
Prepend controller path to lookup_context prefixes
Browse files Browse the repository at this point in the history
Rails rendering assumes that the first prefix encountered is 'special', specifically, it's the controller's prefix. This can be seen in `ActionView::AbstractRenderer::ObjectRendering#initialize`. This change injects the controller path at the start of the prefix list instead of the end to make sure this assumption is satisfied.

Resolves rspec#2729.
  • Loading branch information
sfnelson committed Apr 10, 2024
1 parent 4aced75 commit 537481e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rspec/rails/example/view_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def _include_controller_helpers

before do
_include_controller_helpers
view.lookup_context.prefixes << _controller_path
view.lookup_context.prefixes.prepend(_controller_path)

controller.controller_path = _controller_path

Expand Down
20 changes: 20 additions & 0 deletions spec/rspec/rails/example/view_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,26 @@ def _view; end # Stub method
expect(failure_reporter.exceptions).to eq []
expect(run_count).to eq 1
end

# Rails expects the first prefix to be the controller path.
# @see ActionView::AbstractRenderer::ObjectRendering#initialize
# Regression test for rspec/rspec-rails#2729
it 'injects controller path as first prefix' do
prefixes = []
RSpec.describe "a view spec" do
include ::RSpec::Rails::ViewExampleGroup

def _controller_path
"example/path"
end

specify do
prefixes = view.lookup_context.prefixes
end
end.run

expect(prefixes).to start_with("example/path")
end
end

describe "#template" do
Expand Down

0 comments on commit 537481e

Please sign in to comment.