2016-09-30 50 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。謝謝! –

相關問題