10
votes

Looks like a bug in RSpec but maybe I'm missing something.

I have a request spec where I post a JSON that contains an array of hashes:

spec/requests/dummy_request_spec.rb:

post "http://my.server.com/some/route", {
  format: :json,
  data: [
    {
      details: {
        param1: 1
      },
    },
    {
      details: {
        param2: 1
      }
    }
  ]
}

For some odd reason, RSpec merges the hashes into one element and then sends them to server. print out of params received in controller:

data: [
  {
    details: {
      param1: 1,
      param2: 2
    },
  },
]

versions: rspec-2.13.0 rails-3.2.10

Very strange!!

Thanks

3

3 Answers

20
votes

Got it! array of hashes is not supported for form-data
RSpec by default posts it as form-data. Solution:

post '...', {...}.to_json, {'CONTENT_TYPE' => "application/json", 'ACCEPT' => 'application/json'}
0
votes

Also, be aware that you have an extra comma:

data: [
  {
   details: {
    param1: 1
   }**,**
  },
  {
   details: {
    param2: 1
   }
  }
 ]
0
votes

your answer helped me answer my own post:

Rails JSON API testing POST request with PARAMS in JSON

you should accept it as correct :) thanks for the help