在范围获取请求中没有路由匹配

时间:2013-12-11 15:27:28

标签: ruby-on-rails ruby-on-rails-4

我将两个参数传递给PATCH请求,我从测试中得到了这个错误:

  1) Error:
ProjectsControllerTest#test_should_update_project:
ActionController::UrlGenerationError: No route matches {:id=>nil, :profile_name=>#<Project id: 980190962, title: "MyString", user_id: 1, description: "MyText", created_at: "2013-12-11 15:20:00", updated_at: "2013-12-11 15:20:00">, :controller=>"projects", :action=>"show", :format=>nil} missing required keys: [:id]
    test/controllers/projects_controller_test.rb:44:in `block in <class:ProjectsControllerTest>'

我很困惑,因为这些测试在另一个应用程序中运行。除非传递了:profile_name参数,因为项目的范围限定为用户。

  test "should update project" do
    sign_in :user, @user
    patch :update, id: @project, profile_name: @user, project: { description: @project.description, title: @project.title, user_id: @project.user_id }
    assert_redirected_to project_path(assigns(:project))
  end

为什么测试没有正确接收:id和:profile_name? 控制器代码在浏览器中工作,而不是在测试中。

由于

1 个答案:

答案 0 :(得分:0)

试试这个

 test "should update project" do
    sign_in :user, @user
    patch :update, id: @project.id, profile_name: @user.name, project: { description: @project.description, title: @project.title, user_id: @project.user_id }
    assert_redirected_to project_path(assigns(:project))
  end