Rails RSpec路由::except中的测试动作不要路由 [英] Rails RSpec Routing: Testing actions in :except do NOT route

查看:103
本文介绍了Rails RSpec路由::except中的测试动作不要路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常简单的问题(我曾经想过),但是我遇到了一些问题:

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

在Rails 3.1.0.rc6 / RSpec 2.6.0中,我正在尝试测试产品资源的路由,路由如下:

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




失败/错误:lambda {get( / products / 1 / edit)}。应该raise_error
预期的异常,但未引发任何异常
#./spec/routing/products_routing_spec.rb:11 :in`block(3个级别)
in'

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

我明白了


失败/错误:get( / products / 1 / edit)。不应该
route_to( products#edit ,:id => 1)
ActionController :: RoutingError:
没有路线匹配 / products / 1 / edit

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.

推荐答案

我不知道知道为什么lambda会失败,但是我不认为rspec-rails dsl会像这样使用。您是否尝试过这种方法?

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.

这篇关于Rails RSpec路由::except中的测试动作不要路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆