Skip to content

Commit 732a922

Browse files
committed
Infer controller.request.path_params["controller"] from the file being
rendered in a view spec. - Closes #172.
1 parent 290357c commit 732a922

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

README.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ View specs live in spec/views, and mix in ActionView::TestCase::Behavior.
148148
end
149149
end
150150

151+
View specs infer the controller name and path from the path to the view
152+
template. e.g. if the template is "events/index.html.erb" then:
153+
154+
controller.controller_path == "events"
155+
controller.request.path_parameters[:controller] == "events"
156+
157+
This means that most of the time you don't need to set these values. When
158+
spec'ing a partial that is included across different controllers, you _may_
159+
need to override these values before rendering the view.
160+
151161
## `assign(key, val)`
152162

153163
Use this to assign values to instance variables in the view:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Feature: view spec infers controller path
2+
3+
Scenario:
4+
Given a file named "app/views/widgets/other.html.erb" with:
5+
"""
6+
<%= link_to "new", :action => "new" %>
7+
"""
8+
And a file named "spec/views/widgets/other.html.erb_spec.rb" with:
9+
"""
10+
require "spec_helper"
11+
12+
describe "widgets/other.html.erb" do
13+
it "includes a link to new" do
14+
render
15+
rendered.should have_selector("a", :href => "/widgets/new")
16+
end
17+
end
18+
"""
19+
When I run "rspec spec/views"
20+
Then the output should contain "1 example, 0 failures"
21+

lib/rspec/rails/example/view_example_group.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def _include_controller_helpers
130130
before do
131131
_include_controller_helpers
132132
controller.controller_path = _controller_path
133+
controller.request.path_parameters["controller"] = _controller_path
133134
end
134135
end
135136

0 commit comments

Comments
 (0)