Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Override append/prepend_view_paths in controller specs

- inject rspec-rails' path delegator resolver
- Closes #359.
  • Loading branch information...
commit 39961a0f6756b5b905846659e1b5842f0d2ad6c2 1 parent 0434b57
@amerine amerine authored dchelimsky committed
View
48 features/controller_specs/isolation_from_views.feature
@@ -5,7 +5,7 @@ Feature: views are stubbed by default
template an action should try to render regardless of whether the template
compiles cleanly.
- NOTE: unlike rspec-rails-1.x, the real template must exist.
+ NOTE: unlike rspec-rails-1.x, the real template must exist.
Scenario: expect template that is rendered by controller action (passes)
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
@@ -47,3 +47,49 @@ Feature: views are stubbed by default
When I run `rspec spec`
Then the output should contain "1 example, 1 failure"
+ Scenario: expect empty templates to render when view path is changed at runtime (passes)
+ Given a file named "spec/controllers/things_controller_spec.rb" with:
+ """
+ require "spec_helper"
+
+ describe ThingsController do
+ describe "custom_action" do
+ it "renders an empty custom_action template" do
+ controller.prepend_view_path 'app/views'
+ controller.append_view_path 'app/views'
+ get :custom_action
+ response.should render_template("custom_action")
+ response.body.should == ""
+ end
+ end
+ end
+ """
+ When I run `rspec spec`
+ Then the examples should all pass
+
+ Scenario: expect template to render when view path is changed at runtime (fails)
+ Given a file named "spec/controllers/things_controller_spec.rb" with:
+ """
+ require "spec_helper"
+
+ describe ThingsController do
+ describe "custom_action" do
+ it "renders the custom_action template" do
+ render_views
+ controller.prepend_view_path 'app/views'
+ get :custom_action
+ response.should render_template("custom_action")
+ response.body.should == ""
+ end
+
+ it "renders an empty custom_action template" do
+ controller.prepend_view_path 'app/views'
+ get :custom_action
+ response.should render_template("custom_action")
+ response.body.should == ""
+ end
+ end
+ end
+ """
+ When I run `rspec spec`
+ Then the output should contain "2 examples, 1 failure"
View
18 lib/rspec/rails/view_rendering.rb
@@ -76,12 +76,30 @@ def find_all(*args)
unless render_views?
@_path_set_delegator_resolver = PathSetDelegatorResolver.new(controller.class.view_paths)
controller.class.view_paths = ::ActionView::PathSet.new.push(@_path_set_delegator_resolver)
+ controller.instance_eval do
+ alias orig_prepend_view_path prepend_view_path
+ alias orig_append_view_path append_view_path
+
+ def prepend_view_path(new_path)
+ _new_path_delegator = PathSetDelegatorResolver.new(::ActionView::FileSystemResolver.new(new_path))
+ lookup_context.view_paths.unshift(*_new_path_delegator)
+ end
+
+ def append_view_path(new_path)
+ _new_path_delegator = PathSetDelegatorResolver.new(::ActionView::FileSystemResolver.new(new_path))
+ lookup_context.view_paths.push(*_new_path_delegator)
+ end
+ end
end
end
after do
unless render_views?
controller.class.view_paths = @_path_set_delegator_resolver.path_set
+ controller.instance_eval do
+ alias prepend_view_path orig_prepend_view_path
+ alias append_view_path orig_append_view_path
+ end
end
end
end
View
7 templates/generate_stuff.rb
@@ -10,5 +10,12 @@
generate('scaffold gadget') # scaffold with no attributes
generate('scaffold admin/accounts name:string') # scaffold with nested resource
+generate('controller things custom_action')
+template_code= <<-TEMPLATE
+ <% raise 'Error from custom_action because we should never render this template....derp derp derp' %>
+TEMPLATE
+
+file "app/views/things/custom_action.html.erb", template_code, {:force=>true}
+
run('rake db:migrate')
run('rake db:test:prepare')
Please sign in to comment.
Something went wrong with that request. Please try again.