Specing the Precedence of Rails Routes

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
/posts/123/comments?sort=date

However, I actually got this:
/comments?sort=date

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?

cheers,
Matt

http://blog.mattwynne.net

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.