rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.16k stars 1.03k forks source link

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

Open sfnelson opened 8 months ago

sfnelson commented 8 months ago

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 https://github.com/rails/rails/issues/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.

https://github.com/rspec/rspec-rails/blob/06d05aba209565f5bc940aa9e67232c8616e5197/lib/rspec/rails/example/view_example_group.rb#L189

pirj commented 7 months ago

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?

sfnelson commented 7 months ago

@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.