Rails RSpec 路由:在 : except do NOT 路由中测试操作

发布于 2024-12-02 06:55:06 字数 926 浏览 1 评论 0原文

相当简单的问题(我曾想过),但我遇到了一些问题:

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩世 2024-12-09 06:55:06

我不知道为什么 lambda 会失败,但我不认为 rspec-rails dsl 打算这样使用。你尝试过这样的事情吗?

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

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?

{ :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.

猫卆 2024-12-09 06:55:06

你有后备路线吗?因为这可以解释为什么没有抛出错误,但实际上尝试评估 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文