[rspec-users] Specing the Precedence of Rails Routes

Matt Wynne matt at mattwynne.net
Fri Sep 19 04:38:21 EDT 2008

Sorry that this is only a tentatively RSpec-related question folks.

I had a bug today which was caused by two routing resources being  
specified in a particular order:

map.resources :comments
map.resources :posts, :has_many => :comments

When I was in the CommentsController, rendering the index action for  
a particular post, I made a link to sort the comments like this:

<%= link_to 'Sort by date', :sort => "date" %>

The idea is that this will then render a link like

However, I actually got this:

It seems that the order of the routing is important here: if I flip  
order of the map.resources calls so that the posts one comes first,  
the link renders as expected.

So I concluded that the bug is caused by the routing code matching  
the first suitable route that it heard about.

My question is about how to reproduce this in a spec.

If I write this:
       route_for(:controller => 'comments', :post_id => '123', :sort  
=> 'date').should == "/posts/123/comments/?sort=date"

The spec passes. So something different is happening when my view is  
rendered, I guess to do with the way the controller and post_id are  
implied from the params when I call link_to.

I can, and will, tighten up the view spec. But any thoughts on how to  
spec the routing specifically so that someone can't re-order these  
map.resources calls and break things?


In case you wondered: The opinions expressed in this email are my own  
and do not necessarily reflect the views of any former, current or  
future employers of mine.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080919/c7f40806/attachment-0001.html>

More information about the rspec-users mailing list