2016-09-30 48 views
3

我已经在Ubuntu 16.04 amd64服务器上使用https://gorails.com/setup/ubuntu/16.04的指示安装了Rails。我使用'rbenv'而不是其他任何选项。集成测试时,出现ActionController :: ParameterMissing,尽管日志相反

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

我然后执行:

$ rails generate scaffold Test name:string age:integer 

然后,我执行:

$ bin/rails generate integration_test tests_post 

我修改测试/集成/ 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)中是否有bug?

+0

你的'TestController'中有什么,特别是72行? –

+0

params.require(:test).permit(:name,:age),但这不是这个问题的本质。亚历山大·安吉利姆(Alexandre Angelim)拥有它......只是改变了我在测试中指定参数的方式。 –

+0

你应该学会如何调试这些问题,而不是记住像这样的某些解决方案......你可以简单地在该行上面放置一个'binding.pry',并检查'params'的当前值 - 解决方案相当明显。 –

回答

2

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

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

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

,你会没事的。

+0

“你不需要换行” - 你这样做,因为rails 5 –

+0

谢谢@SergioTulentsev。我会更新答案。 –

+0

是的,这就是答案。我使用4.2,显然我所检查的所有样本都是5.0。谢谢! –

相关问题