Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec request test merges hashes in array in POST JSON params

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

like image 260
Adam Klein Avatar asked Aug 20 '13 14:08

Adam Klein


1 Answers

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'}
like image 128
Adam Klein Avatar answered Oct 26 '22 23:10

Adam Klein