Alex N.
Alex N.

Reputation: 173

Params missing error when sending raw data in JSON Post for RSpec

I am trying to send some raw data in a JSON post request to my RSpec controller test. I have successfully tested the controller with an actual Postman request but I can't get the Rspec test to work

The error I am getting is

param is missing or the value is empty: annotations

Here is my test set-up, which I have copied verbatimly from rails server log when I run the request via Postman

params = '{annotations"=>[{"id"=>1, "location_start"=>1, "location_end"=>3, "source_text"=>"what", "reading"=>"cool"}, {"id"=>2, "location_start"=>1, "location_end"=>-1, "reading"=>"cool"}]}'
patch :update, params, format: :json

I have also tried

params = {:annotations => [{ :id=>1, :location_start=>1, :location_end=>3}]}
patch :update, params.to_json

Upvotes: 0

Views: 744

Answers (1)

jfornoff
jfornoff

Reputation: 1368

https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/controller-spec#setting-a-different-content-type-for-example-json-(request-type)

Check out how the json body is defined in this example: It's a ruby hash! So you need to not try passing in a raw string but a Hash that then gets automatically converted to JSON for you.

Hope that helps you out

Upvotes: 1

Related Questions