Joseph Van Riper Joseph Van Riper - 2 months ago 14
Ruby Question

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

I have installed Rails using the instructions from https://gorails.com/setup/ubuntu/16.04 on an Ubuntu 16.04 amd64 server. I used 'rbenv' rather than any of the other options.

I created a new application named 'testapp'.

I then executed:

$ rails generate scaffold Test name:string age:integer


Then, I executed:

$ bin/rails generate integration_test tests_post


I modified test/integration/tests_post_test.rb to read as follows:

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


And then I executed:

rake test


This gives me the following error:

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


Yet, the relevant logging shows the following:

--------------------------------------
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)


In development and in production, this gives no errors, only in test. I keep looking over examples I've seen online in other projects, but I don't see that I'm doing anything particularly out of the ordinary here. I created these specific steps to show the problem as simply as possible. Any ideas how to get this to work properly? Is there perhaps a bug in this version of rake (11.3.0)?

Answer

You're probably using Rails 4(given the installation tutorial you're following). For Rails 4 you don't need to wrap your params when posting on integration tests. Change the line:

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

to

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

and you'll be fine.