Rails RSpec Routing: Testing actions in :except do NOT route

↘锁芯ラ 提交于 2019-12-01 01:09:52

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7219176/rails-rspec-routing-testing-actions-in-except-do-not-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!