Rails RSpec 路由:在 : except do NOT 路由中测试操作
相当简单的问题(我曾想过),但我遇到了一些问题:
在 Rails 3.1.0.rc6/RSpec 2.6.0 中,我正在尝试测试“产品”资源的路由,路由如下:
resources :products, :except => [:edit, :update]
有效操作的路由有效,但我想确保编辑和更新路由不可调用。这就是我正在尝试的:
it "does not route to #edit" do
lambda { get("/products/1/edit") }.should raise_error
end
失败/错误:lambda { get("/products/1/edit") }.should raise_error 预期异常但没有引发任何异常 # ./spec/routing/products_routing_spec.rb:11:in `块(3 级) 在 '
...然而,当我跑步时
it "does not route to #edit" do
get("/products/1/edit").should_not route_to("products#edit", :id => "1")
end
我得到
失败/错误:get("/products/1/edit").should_not Route_to("产品#edit", :id => "1") ActionController::路由错误: 没有路由匹配“/products/1/edit”
知道这里发生了什么吗?我猜这应该很简单,但我似乎无法弄清楚。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道为什么 lambda 会失败,但我不认为 rspec-rails dsl 打算这样使用。你尝试过这样的事情吗?
http://relishapp.com/rspec/rspec-rails /docs/routing-specs/be-routable-matcher
所以你不能指定它不路由到什么,但你可以指定它不被路由。
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?
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.
你有后备路线吗?因为这可以解释为什么没有抛出错误,但实际上尝试评估
route_to("products#edit", :id => 1)
会引发错误,因为路由不存在。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.