How do I spec my Rails routes?

Once again, here’s something I haven’t figured out. Apologies if this
is too newb-ish, but, how do I spec my routes using RSpec-Rails?

In particular, I’ve got a catch-all route that needs to catch a wide
variety of URLs:

map.document ‘*url’, :controller => ‘documents’, :action => ‘show’

Is there a way to have a controller spec test site-wide routing? It
seems to me that the get and post methods are just controller-
specific routing. If I can’t do it in a controller spec, where else
should I do it?

Thanks,

Francis H.
http://fhwang.net/

On Dec 16, 2007 10:32 AM, Francis H. [email protected] wrote:

specific routing. If I can’t do it in a controller spec, where else
should I do it?

Check out route_for and params_from at http://rspec.info/rdoc-rails/.
There are examples of their use in the controller examples generated
by ‘script/generate rspec_scaffold …’. Here are a couple of
examples:

http://pastie.caboo.se/129301

Cheers,
David

personally I’ve broken with convention and put all my route specs
into a routes_spec.rb file
in part because they’re in one file in rails, and when i edit that
file I want to make sure other routes didn’t break.
Your question about a catchall is another reason.

I also like to spec named routes, not all the time but especially
those outside the rails resources conventions. Although not strictly
a “behavior”, I’m more comfortable knowing that the named routes work
(e.g. logout_path => /logout ) before using them in my code.
Unfortunately I found (via trial and error) that you need a response
first. This is what I have working (although its hackish):

class RoutesController < ApplicationController
def foo
end
end

describe FoosController, “named routes” do
before do
get :foo
end

it "should route /logout " do
logout_path.should == “/logout”
route_for(:controller => “sessions”, :action =>
“destroy”).should == “/logout”
params_from(:get, “/logout”).should == {:controller =>
“sessions”, :action => “destroy”}
end

end

–linoj

On Dec 16, 2007 3:23 PM, Jonathan L. [email protected]
wrote:

personally I’ve broken with convention and put all my route specs
into a routes_spec.rb file

1.1 broke with that same convention! But slightly differently. The
rspec_scaffold generator generates a controller spec and a routing
spec for each controller you create.

it "should route /logout " do
logout_path.should == “/logout”
route_for(:controller => “sessions”, :action =>
“destroy”).should == “/logout”
params_from(:get, “/logout”).should == {:controller =>
“sessions”, :action => “destroy”}
end

end

What would you like to see in rspec to manage this for you? How about
a feature request in the tracker?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs