集成测试时,ActionController::ParameterMissing 出现,尽管记录相反

While integration testing, ActionController::ParameterMissing appears, despite logs to the contrary

我已经使用 https://gorails.com/setup/ubuntu/16.04 中的说明在 Ubuntu 16.04 amd64 服务器上安装了 Rails。我使用了 'rbenv' 而不是任何其他选项。

我创建了一个名为 'testapp' 的新应用程序。

然后我执行了:

$ rails generate scaffold Test name:string age:integer

然后,我执行了:

$ bin/rails generate integration_test tests_post

我修改test/integration/tests_post_test.rb如下:

require 'test_helper'

class TestsPostTest < ActionDispatch::IntegrationTest

  test "can create an item" do
    get "/tests/new"
    assert_response :success
    post "/tests",
      params: { test: { name: 'Micky Mouse', age: 120 } }
    assert_response :redirect
    follow_redirect!
    assert_response :success
  end
end

然后我执行了:

rake test

这给了我以下错误:

Run options: --seed 24994

# Running:

.......E

Finished in 0.247049s, 32.3822 runs/s, 56.6689 assertions/s.

  1) Error:
TestsPostTest#test_can_create_an_item:
ActionController::ParameterMissing: param is missing or the value is empty: test
    app/controllers/tests_controller.rb:72:in `test_params'
    app/controllers/tests_controller.rb:27:in `create'
    test/integration/tests_post_test.rb:8:in `block in <class:TestsPostTest>'

8 runs, 14 assertions, 0 failures, 1 errors, 0 skips

然而,相关日志显示如下:

--------------------------------------
TestsPostTest: test_can_create_an_item
--------------------------------------
Started GET "/tests/new" for 127.0.0.1 at 2016-09-30 07:50:18 -0400
Processing by TestsController#new as HTML
  Rendered tests/_form.html.erb (13.6ms)
  Rendered tests/new.html.erb within layouts/application (16.4ms)
Completed 200 OK in 170ms (Views: 161.1ms | ActiveRecord: 0.0ms)
Started POST "/tests" for 127.0.0.1 at 2016-09-30 07:50:19 -0400
Processing by TestsController#create as HTML
  Parameters: {"params"=>{"test"=>{"name"=>"Mickey Mouse", "age"=>"120"}}}
Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms)

在开发和生产中,这没有错误,只有在测试中。我一直在查看我在其他项目中在线看到的示例,但我没有发现我在这里做了什么特别不寻常的事情。我创建这些特定步骤是为了尽可能简单地说明问题。任何想法如何让它正常工作?这个版本的 rake (11.3.0) 可能有错误吗?

您可能正在使用 Rails 4(考虑到您正在学习的安装教程)。对于 Rails 4,在发布集成测试时不需要包装参数。更改行:

post "/tests", params: { test: { name: 'Micky Mouse', age: 120 } }

post "/tests", { test: { name: 'Micky Mouse', age: 120 } }

你会没事的。