[rspec-devel] [ rspec-Patches-6471 ] Easy way to spec routes

noreply at rubyforge.org noreply at rubyforge.org
Thu Nov 9 19:37:08 EST 2006


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

>Category: rails plugin
Group: None
>Status: Closed
>Resolution: Accepted
Priority: 3
Submitted By: Pat Maddox (pergesu)
>Assigned to: Aslak Hellesøy (aslak_hellesoy)
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 Hellesøy (aslak_hellesoy)
Date: 2006-11-09 19:37

Message:
Applied in r1076

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

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-11-09 09:11

Message:
I'm moving this to a patch. This patch must be applied in order to get rspec_resource to work (http://rubyforge.org/tracker/?func=detail&atid=3152&aid=6098&group_id=797)

I'm not applying it yet since there are no specs, and it doesn't seem to work with Rails 1.1.6 (the latest release as of this writing)

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

Comment By: Pat Maddox (pergesu)
Date: 2006-11-04 23: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 21: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 21: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 Hellesøy (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=3151&aid=6471&group_id=797


More information about the rspec-devel mailing list