ruby-on-rails – Rails RSpec路由:测试操作:除了不路由

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

在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

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 ‘

……然而,当我跑的时候

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

我明白了

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

知道这里发生了什么吗?我猜这应该很简单,但我似乎无法弄明白.

解决方法

我不知道为什么lambda会失败,但我不认为rspec-rails dsl是打算像那样使用.你尝试过这样的事吗?
{ :get => "/products/1/edit" }.should_not be_routable

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

因此,您无法指定它未路由到的内容,但您可以指定它不会被路由.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...