rails: Sending JSON in functional tests does not populate the params hash
test 'sending raw JSON does not populate the params hash' do
@request.headers['Content-Type'] = 'application/json'
post :create, { key: "value" }.to_json
assert_equal "value", params['key'] # => Will fail!
end
will result in following params
hash:
{"controller"=>"posts", "action"=>"create"} # Hence the missing key-value pair.
However sending the corresponding request with curl or other apps, it works like intended. I even asked on StackOverflow, however no answer yet. Looks like a bug for me.
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Comments: 31 (23 by maintainers)
Commits related to this issue
- allow sending documents in AC::TestCase#post and friends. — committed to rails/rails by apotonick 13 years ago
- In AC::TestCase#process, parse the request body to params as it is done in the ParamsParser middleware. fixes #13135. this is another work-around to make AC::TestCase behave the way a real integratio... — committed to apotonick/rails by apotonick 11 years ago
In case anyone end up here via Google (like I did), the correct way to do this in Rails 5 is:
this is only marginally related to the question, but hopefully helpful to others with the same question as I had
I started to test with Rack-test. It is a million times faster, no magic and does exactly what you would think it does.