[rspec-users] Problem verifying routing error

r_j_h_box-sf at yahoo.com r_j_h_box-sf at yahoo.com
Tue Jun 16 16:07:13 EDT 2009


I finally figured this out.

lambda { route_for(:controller => "designs", :action => "create").should == "anything" }.should raise_error( ActionController::RoutingError )

The clue was that I wasn't getting a routing error until I tried to compare route_for() with something.  route_for() seems to generate an object that overrides ==(), and at that time it does raise the exception.  Now we wrap that comparison in a lambda and assert that the *comparison* should raise the expected routing error.

So - great, we can actually test it.  But the syntax does leave something to be desired.  dchelimsky, can you recommend any alternatives that would be a bit cleaner for testing that a route doesn't exist?

Thanks,

Randy




----- Original Message ----
> From: Ben Mabey <ben at benmabey.com>
> To: r_j_h_box-sf at yahoo.com; rspec-users <rspec-users at rubyforge.org>
> Sent: Friday, May 8, 2009 10:25:03 AM
> Subject: Re: [rspec-users] Problem verifying routing error
> 
> Randy Harmon wrote:
> > Hi,
> >
> > When upgrading to rspec/rspec-rails 1.2.6 gem (from 1.1.12), I'm having
> > a new problem verifying routes that should not exist.
> >
> > This is to support something like this in routes.rb:
> >
> > map.resources :orders do |orders|
> >     orders.resources :items, :except => [:index,:show]
> > end
> >
> > I used to use lambda {}.should_raise( routing error ), but it stopped
> > detecting any raised error.  Requesting it through the browser produces
> > ActionController::MethodNotAllowed (Only post requests are allowed). But
> > that error wasn't detected.
> >
> > When I skip the lambda, and just ask it to verify that the route does
> > exist (which *should* fail), I get the same result for those :except
> > actions as for a made-up action name.  Seems this must have something to
> > do with the change in how route_for delegates back to ActionController's
> > routing assertion (sez the backtrace :).
> >
> >
> > NoMethodError in 'ItemsController route generation should NOT map
> > #indewfefwex'
> > You have a nil object when you didn't expect it!
> > You might have expected an instance of Array.
> > The error occurred while evaluating nil.first
> > 
> /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/assertions/routing_assertions.rb:134:in
> > `recognized_request_for'
> > 
> /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/assertions/routing_assertions.rb:49:in
> > `assert_recognizes'
> > 
> /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/assertions.rb:54:in
> > `clean_backtrace'
> > 
> /Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/assertions/routing_assertions.rb:47:in
> > `assert_recognizes'
> > ./spec/controllers/thoughts_routing_spec.rb:9:
> >
> >
> > I tried using bypass_rescue in my routing/items_routing_spec.rb file as
> > mentioned by the upgrade doc, but it wasn't valid in the "routing" spec
> > - worked fine when I moved the file back to spec/controllers/, though. 
> > Seems like that's not the issue, but I'm mentioning for more completeness.
> >
> > Any ideas what I should be doing instead, or how I can troubleshoot further?
> >  
> 
> 
> Hmm.. yeah, it seems like it might have to do with how the exceptions 
> are being handled in the newer version of rspec-rials (see 
> https://rspec.lighthouseapp.com/projects/5645/tickets/85-11818-have-mode-for-rails-error-handling).  
> 
> I don't use RSpec to verify my routes very often and have never used it 
> to verify the non-existence of a route so I'm afraid I don't really have 
> any ideas...
> 
> Does anyone else have an idea to do this?
> 
> -Ben



More information about the rspec-users mailing list