Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rails 7.1.alpha1 (main): controller specs crash because of ResolverDecorator != Resolver class hierarchy #2659

Closed
zealot128 opened this issue Mar 14, 2023 · 2 comments

Comments

@zealot128
Copy link

What Ruby, Rails and RSpec versions are you using?

Ruby version: 2.7, 3.0
Rails version: 7.1.alpha1 (git main branch)
RSpec version: core: 3.12.1,, rspec-rails 4.1.2

  • rails-controller

Observed behaviour

After running controller tests, every controller test fails immediately with:

  Failure/Error: raise TypeError, "#{path.inspect} is not a valid path: must be a String, Pathname, or Resolver"

     TypeError:
       #<RSpec::Rails::ViewRendering::EmptyTemplateResolver::ResolverDecorator:0x00007fee250ed038 @resolver=#<ActionView::FileSystemResolver:0x00007fee3763a740 @unbound_templates=#<Concurrent::Map:0x00007fee3763a718 entries=0 default_proc=nil>, @path_parser=#<ActionView::Resolver::PathParser:0x00007fee3763a6a0>, @path="/Users/.../app/views"
>> is not a valid path: must be a String, Pathname, or Resolver
  # /Users/../bundler/gems/rails-3025eaa09154/actionview/lib/action_view/path_set.rb:74:in `block in typecast'
  # /Users/../bundler/gems/rails-3025eaa09154/actionview/lib/action_view/path_set.rb:67:in `map'
  # /Users/../bundler/gems/rails-3025eaa09154/actionview/lib/action_view/path_set.rb:67:in `typecast'
  # /Users/../bundler/gems/rails-3025eaa09154/actionview/lib/action_view/path_set.rb:19:in `initialize'
  # /Users/../bundler/gems/rails-3025eaa09154/actionview/lib/action_view/view_paths.rb:62:in `new'
  # /Users/../bundler/gems/rails-3025eaa09154/actionview/lib/action_view/view_paths.rb:62:in `view_paths='
  # /Users/../gems/rspec-rails-4.1.2/lib/rspec/rails/view_rendering.rb:153:in `block (2 levels) in <module:ViewRendering>'
  # /Users/../gems/rspec-core-3.12.1/lib/rspec/core/example.rb:457:in `instance_exec'
  # /Users/../gems/rspec-core-3.12.1/lib/rspec/core/example.rb:457:in `instance_exec'
  # /Users/../gems/rspec-core-3.12.1/lib/rspec/core/hooks.rb:365:in `run'

After investigating, it seems to be that the ActionView::PathSet#typecast method seems to check for class ancestors ("Resolver"), but the RSpec::Rails::ViewRendering::EmptyTemplateResolver::ResolverDecorator of rspec-rails does not fit that bill, so it crashes here.

I've found a workaround by monkey patching the typecast method, e.g. by pasting into spec_helper.rb and adding " RSpec::Rails::ViewRendering::EmptyTemplateResolver::ResolverDecorator" to the case-when clause:

module ControllerViewPatch
  extend ActiveSupport::Concern

  included do
    def typecast(paths)
      paths.map do |path|
        case path
        when Pathname, String
          ActionView::PathRegistry.file_system_resolver(path.to_s)
        when ActionView::Resolver, RSpec::Rails::ViewRendering::EmptyTemplateResolver::ResolverDecorator
          path
        else
          raise TypeError, "#{path.inspect} is not a valid path: must be a String, Pathname, or Resolver"
        end
      end
    end
  end
end

require 'action_view/path_set'
ActionView::PathSet.include(ControllerViewPatch)
@JonRowe
Copy link
Member

JonRowe commented Mar 14, 2023

This was fixed in #2631, if you upgrade rspec-rails to main you should find you don't need that patch. (By the way 4.1.2 only supports Rails 5 and 6).

@JonRowe JonRowe closed this as completed Mar 14, 2023
@zealot128
Copy link
Author

@JonRowe thanks for the tip, I had a version constraint in a deeper dep on rspec-rails, that's why I didn't pull the newest rspec-rails when upgrading rails..
Sorry for the distraction :)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants