[rspec-users] Help with nested routes test in Rails 3

179 views
Skip to first unread message

Lalish-Menagh, Trevor

unread,
Jul 3, 2010, 2:40:20 AM7/3/10
to rspec...@rubyforge.org
Hi all,

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:
1) 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.

Yours,
Trevor
--
Trevor Lalish-Menagh
484.868.6150 mobile
tr...@trevreport.org
http://www.trevmex.com/
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Lalish-Menagh, Trevor

unread,
Jul 4, 2010, 4:28:39 PM7/4/10
to rspec...@rubyforge.org
Hi again,

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

David Chelimsky

unread,
Jul 5, 2010, 8:04:42 AM7/5/10
to rspec-users

Please join the conversation about improving the API for routing specs: http://groups.google.com/group/rspec/browse_thread/thread/50b46ca3e4bd3a78

Cheers,
David

Reply all
Reply to author
Forward
0 new messages