Skip to content

Commit

Permalink
Rails removed unshift for view_paths, use their copy constructor instead
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Jun 28, 2023
1 parent 0717183 commit 4d65bea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
28 changes: 20 additions & 8 deletions lib/rspec/rails/example/view_example_group.rb
Expand Up @@ -82,14 +82,26 @@ def view
_view
end

# Simulates the presence of a template on the file system by adding a
# Rails' FixtureResolver to the front of the view_paths list. Designed to
# help isolate view examples from partials rendered by the view template
# that is the subject of the example.
#
# stub_template("widgets/_widget.html.erb" => "This content.")
def stub_template(hash)
view.view_paths.unshift(StubResolverCache.resolver_for(hash))
if ::Rails.version.to_f >= 7.1
# Simulates the presence of a template on the file system by adding a
# Rails' FixtureResolver to the front of the view_paths list. Designed to
# help isolate view examples from partials rendered by the view template
# that is the subject of the example.
#
# stub_template("widgets/_widget.html.erb" => "This content.")
def stub_template(hash)
view.view_paths.send(:initialize_copy, ActionView::PathSet.new([StubResolverCache.resolver_for(hash)] + view.view_paths.paths))
end
else
# Simulates the presence of a template on the file system by adding a
# Rails' FixtureResolver to the front of the view_paths list. Designed to
# help isolate view examples from partials rendered by the view template
# that is the subject of the example.
#
# stub_template("widgets/_widget.html.erb" => "This content.")
def stub_template(hash)
view.view_paths.unshift(StubResolverCache.resolver_for(hash))
end
end

# Provides access to the params hash that will be available within the
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/rails/example/view_example_group_spec.rb
Expand Up @@ -283,7 +283,7 @@ def _view; end
Class.new do
include ViewExampleGroup::ExampleMethods
def _view
@_view ||= Struct.new(:view_paths).new(['some-path'])
@_view ||= Struct.new(:view_paths).new(ActionView::PathSet.new(['some-path']))
end
end
end
Expand Down

1 comment on commit 4d65bea

@jrochkind
Copy link
Contributor

@jrochkind jrochkind commented on 4d65bea Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix for Rails 7.1!

This unreleased commit is of course needed for stub_template feature to work with recently released Rails 7.1.0.

Is there any chance of an rspec-rails release soon that will include this?

It is cumbersome to try using rspec-rails from unreleased git main, due to dependency tree of other unreleased rspec-* gems.

Please sign in to comment.