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