Skip to content

Commit 537481e

Browse files
committed
Prepend controller path to lookup_context prefixes
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#2729.
1 parent 4aced75 commit 537481e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

‎lib/rspec/rails/example/view_example_group.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def _include_controller_helpers
186186

187187
beforedo
188188
_include_controller_helpers
189-
view.lookup_context.prefixes << _controller_path
189+
view.lookup_context.prefixes.prepend(_controller_path)
190190

191191
controller.controller_path=_controller_path
192192

‎spec/rspec/rails/example/view_example_group_spec.rb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,26 @@ def _view; end # Stub method
255255
expect(failure_reporter.exceptions).toeq[]
256256
expect(run_count).toeq1
257257
end
258+
259+
# Rails expects the first prefix to be the controller path.
260+
# @see ActionView::AbstractRenderer::ObjectRendering#initialize
261+
# Regression test for rspec/rspec-rails#2729
262+
it'injects controller path as first prefix'do
263+
prefixes=[]
264+
RSpec.describe"a view spec"do
265+
include ::RSpec::Rails::ViewExampleGroup
266+
267+
def_controller_path
268+
"example/path"
269+
end
270+
271+
specifydo
272+
prefixes=view.lookup_context.prefixes
273+
end
274+
end.run
275+
276+
expect(prefixes).tostart_with("example/path")
277+
end
258278
end
259279

260280
describe"#template"do

0 commit comments

Comments
(0)