10

This is a problem that has been bothering me for some time. I am building an API function that should receive data in json and response in json. My controller tests run fine(Since I abstract that the data gets there already decode from JSON and only the answer needs to be interpreted ).

I Also know that the function runs fine since I have used curl to test it with JSON arguments and it works perfectly. (ex: curl -i --header "Accept: application/json" --header "Content-Type: application/json" -d '{"test":{"email":"[email protected]"}}' )

But obviously I would like to write request(feature) tests to test this automatically and the way I see it they should work exactly like curl, i.e., hit my service like it was an external call. That means that I would like to pass the arguments in JSON and receive an answer. I am pretty lost since all the examples I can see people treat arguments as it was already decoded.

My question is: I am following a wrong premise in wanting to send the arguments and request as a JSON one since i will be testing that rails works, because this is its responsibility? But I would like to see how robust my code his to wrong arguments and would like to try with JSON.

something of this type:

it "should return an error if there is no correct email" do
    params = {:subscription => {:email => "andre"}}

    post "/magazine_subscriptions", { 'HTTP_ACCEPT' => "application/json", 'Content-Type' => 'application/json', 'RAW_POST_DATA' => params.to_json }
end

Do you know how this is possible? and please let me know if you think I am testing it wrong.

all the best,

Andre

2 Answers 2

19

I found my answer on a post here(RSpec request test merges hashes in array in POST JSON params), I think what I was doing wrong concerned the arguments to the request.

so this worked:

it "should return an error if there is no correct email" do
    params = {:subscription => {:email => "andre"}}

    post "/magazine_subscriptions", params.to_json, {'ACCEPT' => "application/json", 'CONTENT_TYPE' => 'application/json'}
end
5
  • 6
    For brevity, write it as post '/magazine_subscriptions', params.to_json, format: :json.
    – user664833
    Commented Nov 1, 2013 at 21:20
  • @user664833 that's more elegant
    – Tudor
    Commented Sep 14, 2015 at 10:45
  • 2
    I don't think that .to_json is necessary if you are already formatting it as a json object. I think it should be post '/magazine_subscriptions', params, format: :json Commented Sep 16, 2015 at 18:22
  • Or post "/magazine_subscriptions.json", params
    – Chris Beck
    Commented May 25, 2017 at 23:47
  • In minitest, Rails 6.0.4.7, format: :json didn't work. I had to use post "url", as: :json, params: {} Commented Feb 22, 2023 at 20:48
1
describe '#create' do 
  let(:email) {'andre'}
  let(:attrs) {{email: email}}
  let(:params) {{format: :json, subscription: attrs}}

  it "should return an error if there is no correct email" do
    post "/magazine_subscriptions", params
  end
end
1
  • 1
    Thanks for this. I had a hard time finding an example for recent versions of rails. post "/magazine_subscriptions", subscription: subscription_params, format: :json was the key for me. No to_json necessary.
    – jrhorn424
    Commented Aug 4, 2015 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.