新手问题 具名路有为何测试通不过?

zix · 2016年06月29日 · 最后由 huacnlee 回复于 2016年06月29日 · 1701 次阅读

按着 ruby on rails tutorial 教程做的 测试部分:

test "should get home" do
  get root_path
  assert_response :success
  assert_select "title", "Ruby on Rails Tutorial Sample App"
end

路有部分:

root 'static_pages#home'

可是测试报错:

ActionController::UrlGenerationError: No route matches {:action=>"/", :controller=>"static_pages"}

这是怎么回事?

测试 get root_path 改成 get :home 就可以通过。 我现在直接访问根目录是能自动跳转的啊。。

把当前测试类的父类修改为集成测试类 (IntegrationTest)。 原因:ControllerTest 类修改了默认的 get 方法,参数不再是路径,而是当前控制器的公开方法,例如:在类 StaticsTest 中的get :index默认访问的是statics#index 因此也就出现的你的结果:程序在 static_pages 控制器中寻找/方法,/是由root_path方法得到

需要 登录 后方可回复, 如果你还没有账号请 注册新账号