0

我正在尝试测试请求根路径 ( /) 路由到我的 beta 控制器的:new操作。当我手动(在我的浏览器中)执行此操作时,它工作正常。但是我的自动化测试失败了No route matches "/"

config/routes.rb我有

MyRailsApp::Application.routes.draw do
  root to: 'beta#new' # Previously: `redirect('/beta/join')`
end

在我spec/routing/root_route_spec.rb尝试过

require 'spec_helper'
describe "the root route" do
  it "should route to beta signups" do
    get('/').should route_to(controller: :beta, action: :new)
  end
end

并且也尝试过

require 'spec_helper'
describe "the root route" do
  it "should route to beta signups" do
    assert_routing({ method: :get, path: '/' }, { controller: :beta, action: :new })                                                                                                                                     
  end
end

但两人都抱怨No route matches "/"

1) the root route should route to the beta signups
   Failure/Error: get('/').should route_to "beta#new"
     No route matches "/"
   # ./spec/routing/root_route_spec.rb:5:in `block (2 levels) in <top (required)>'

当我在浏览器中转到 时localhost:3000,我已正确路由到该BetaController::new操作。

什么解释了No route matches "/"错误?

我正在使用 Rails 3.1.3 和 RSpec-2.10。

谢谢!

4

1 回答 1

2

您应该测试/重定向到/beta/join,然后作为单独的问题测试/beta/join路由到控制器的:new操作。:beta

重定向是在 中测试的requests,而不是routing.

# spec/requests/foobar_spec.rb
describe 'root' do
  it "redirects to /beta/join" do
    get "/"
    response.should redirect_to("/beta/join");
  end
end

# spec/routing/beta_spec.rb
...
it 'routes /beta/join to the new action'
  get('beta/join').should route_to(:controller => 'beta', :action => 'new')
end
于 2012-06-18T11:22:39.027 回答