根路由上的Rails assert_routing引发了ActionController :: RoutingError

时间:2012-06-17 21:27:40

标签: ruby-on-rails-3 rspec2

我正在尝试测试请求根路径(/)路由到我的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。

谢谢!

1 个答案:

答案 0 :(得分: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