8

Fairly simple problem (I'd have thought), but I'm having some issues:

In Rails 3.1.0.rc6/RSpec 2.6.0, I'm trying to test the routing of my 'products' resource, routed like this:

resources :products, :except => [:edit, :update]

The routing for the valid actions works, but I want to ensure that the edit and update routes are not callable. Here's what I'm trying:

it "does not route to #edit" do
  lambda { get("/products/1/edit") }.should raise_error
end

Failure/Error: lambda { get("/products/1/edit") }.should raise_error expected Exception but nothing was raised # ./spec/routing/products_routing_spec.rb:11:in `block (3 levels) in '

...And yet, when I run

it "does not route to #edit" do
  get("/products/1/edit").should_not route_to("products#edit", :id => "1")
end

I get

Failure/Error: get("/products/1/edit").should_not route_to("products#edit", :id => "1") ActionController::RoutingError: No route matches "/products/1/edit"

Any idea what's going on here? I'm guessing this should be pretty simple, but I can't seem to figure it out.

PlankTon
  • 12,443
  • 16
  • 84
  • 153

2 Answers2

11

I don't know why the lambda would fail, but I don't think the rspec-rails dsl is intended to be used like that. Have you tried something like this?

{ :get => "/products/1/edit" }.should_not be_routable

http://relishapp.com/rspec/rspec-rails/docs/routing-specs/be-routable-matcher

So you can't specify what it doesn't route to, but you can specify that it doesn't get routed.

nruth
  • 1,068
  • 7
  • 22
  • We have a winner. Strange - I think Rails 2 used to handle "raise error" lambdas, but clearly not 3.1. Wasn't even aware of the be_routable method - many thanks. – PlankTon Aug 28 '11 at 14:31
  • 1
    Very useful! Just one quick note: the above test MUST be in /spec/routing/xxxx.rb or it will fail with an error. You can't, for instance, have this in /spec/requests/xxxx.rb – Dave Collins Dec 05 '12 at 22:38
0

Do you have a fallback route? Because that would explain why no error is thrown, but indeed trying to evaluate route_to("products#edit", :id => 1) would raise, because the route does not exist.

nathanvda
  • 49,707
  • 13
  • 117
  • 139
  • Nice thought, but no. This is a bare project - literally started this afternoon. No redirects set up - at the moment it's just untouched bare scaffolded resources, rspec & cucumber. – PlankTon Aug 28 '11 at 08:08