I must be doing something wrong here. Could someone point me in the
right direction? I am trying to test my nested resources in Rails 3.
The route:
resources :students do
resources :days
end
The test:
it “recognizes and generates #index” do
{ :get => “/students/1/days” }.should route_to(:controller =>
“days”, :action => “index”, :student_id => “1”)
end
The error:
DaysController routing nested in students recognizes and generates #index
Failure/Error: { :get => “/students/1/days” }.should
route_to(:student_id => “1”, :controller => “days”, :action =>
“index”)
found extras <{:student_id=>“1”}>, not <{}>
The route works find in the browser, I just don’t know what I am
missing to make the test pass.
I figured out the issue. RSpec’s route_to wraps Rails’ assert_routing,
which checks two things:
Rails recognizes the given path and routes it to a particular spot
in your application (via assert_recognizes)
a particular set of options generate a particular path (via
assert_generates)
The problem (in my example below) is that the set of options given:
{:controller => “days”, :action => “index”, :student_id => “1”}
generates the path:
“/days”
not:
“/student/1/days”
Rails doesn’t take into account the foreign key of nested resources
when generating a path from a set of options.
Now that I understand this, I can write a test just using
assert_recognizes that passes:
assert_recognizes({:controller => “days”, :action => “index”,
:student_id => “1”}, “/students/1/days”)
Hooray!
I hope this helps someone else out there. It was really confusing me.
Yours,
Trevor
On Sat, Jul 3, 2010 at 2:40 AM, Lalish-Menagh, Trevor [email protected] wrote: