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

noreply at rubyforge.org noreply at rubyforge.org
Sat Nov 4 23:20:42 EST 2006


Feature Requests item #6471, was opened at 2006-11-03 11: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: Pat Maddox (pergesu)
Date: 2006-11-04 21:20

Message:
re: testing rails.  It's basically the same as when you have
validations set up in your model.  You test to make sure
those are working, right?  It's certainly behavior, even if
Rails takes care of the hard part.

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

Comment By: Lachie Cox (lachie)
Date: 2006-11-04 19:06

Message:
There's also the opposite operation of recognising a url using the routing patterns... also produces matched params hash, plus an "extras" hash.

Like
specify "should recognise /events/1;edit as { :controller => 'events', :action => 'edit', :id => 1 }" do
    route_recognise("/events/1;edit").should_equal(:controller => "events", :action => "edit", :id => 1)
end

This kind of expectation might be mooted by integration speccing.

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

Comment By: Lachie Cox (lachie)
Date: 2006-11-04 19:01

Message:
I had a look at this too a while back. I think there needs to be a second argument to #routing through which you can send the "extra" parameters.

I was assuming that these are arbitrary parameters which don't fit into the rest of the route pattern, and are generated out as the query of the url. Does that sound right?

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

Comment By: Aslak Hellesoy (aslak_hellesoy)
Date: 2006-11-04 14: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 13: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