rspec/rspec-rails

View example group inserts controller path into lookup_context in the wrong order

Opened this issue · 2 comments

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.2.3
Rails version: 7.1.3
RSpec version: 3.12.2

Observed behaviour

Testing a controller where the 'prefixes' logic in ActionView::Renderer::AbstractRenderer#merge_prefix_into_object_path applies fails to find view partials when called from inside a nested controller.

This happens because the logic uses @context_prefix = lookup_context.prefixes.first, which in view specs is "", instead of the controller path.

Expected behaviour

@context_prefix = lookup_context.prefixes.first should return the controller's path

Can you provide an example reproduction?

Hard to reproduce as it relates to rails/rails#50916 – the logic in merge_prefix_into_object_path isn't very useful. I've got a monkey patch for that logic that address the issue, which is how I came across this issue.

I believe the logic in view_example_group is faulty because it uses append instead of prepend when setting up the lookup context prefixes.

view.lookup_context.prefixes << _controller_path

pirj commented

So if you change it to prepend (unshift) instead of << in rspec-rails code itself, would it fix your issue?

Controller path taking precedence over a zero prefix sounds logical.
But we’ll need to figure out a spec to indicate what we’re fixing.

Would you like to submit a PR?

@pirj yes, changing line 189 to unshift instead of << will fix the issue. I'll see if I can create a reproduction and a PR.