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

David Chelimsky dchelimsky at gmail.com
Mon Jul 5 08:04:42 EDT 2010


On Jul 4, 2010, at 3:28 PM, Lalish-Menagh, Trevor wrote:
> 
> On Sat, Jul 3, 2010 at 2:40 AM, Lalish-Menagh, Trevor
> <trev at trevreport.org> wrote:
>> 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
> 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

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

Cheers,
David


More information about the rspec-users mailing list