[rspec-devel] [ rspec-Feature Requests-6471 ] Easy way to spec routes

noreply at rubyforge.org noreply at rubyforge.org
Sat Nov 4 16:38:26 EST 2006


Feature Requests item #6471, was opened at 2006-11-03 13:14
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=6471&group_id=797

Category: None
Group: None
Status: Open
Priority: 3
Submitted By: Pat Maddox (pergesu)
Assigned to: Nobody (None)
Summary: Easy way to spec routes

Initial Comment:
I'd like to specify that my routes are generating correctly, as shown below:

context "The EventsController" do
  controller_name :events

  specify "should be a EventsController" do
    controller.should_be_an_instance_of EventsController
  end
  
  specify "should map { :controller => 'events', :action => 'index' } to /events" do
    routing(:controller => "events", :action => "index").should_equal "/events"
  end
  
  specify "should map { :controller => 'events', :action => 'show', :id => 1 } to /events/1" do
    routing(:controller => "events", :action => "show", :id => 1).should_equal "/events/1"
  end
  
  specify "should map { :controller => 'events', :action => 'edit', :id => 1 } to /events/1;edit" do
    routing(:controller => "events", :action => "edit", :id => 1).should_equal "/events/1;edit"
  end
  
  specify "should map { :controller => 'events', :action => 'new' } to /events/new" do
    routing(:controller => "events", :action => "new").should_equal "/events/new"
  end
end



I made it work by adding the following method to Spec::Runner::ContextEval::InstanceMethods

def routing(options)
  # Load routes.rb if it hasn't been loaded
  ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
  ActionController::Routing::Routes.generate(options)
end


Sorry I don't have any specs for it..the rspec_rails 0.6.4 plugin doesn't have any specs to add to.  Not sure how robust this will be (it only uses a tiny part of the code from Rails' assert_routing), but so far it's worked for all of my basic routes.

----------------------------------------------------------------------

>Comment By: Aslak Hellesoy (aslak_hellesoy)
Date: 2006-11-04 16:38

Message:
This is very useful for verifying custom routes (config/routes.rb) , which is rather common in Rails apps.

It's not testing rails, it's testing *your* routes.

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2006-11-04 15:02

Message:
This seems like you're testing rails as opposed to specifying the behaviour of your code.

Am I missing something?

David

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=6471&group_id=797


More information about the rspec-devel mailing list