Romanos
Romanos

Reputation: 149

Rails 5.1 minitest flattens array of arrays in params

After upgrading from Rails 4.2 to 5.1 it seems that only for the tests, when I provide something like:

post :create, params: {
  model: {
    prices: [[2000, "EUR"], [2113.56, "USD"]],
    estimates: [[50, 2500, "EUR"], [505, 2600, "USD"]]
  }
}

It is then parsed within the params of the controller as:

prices: [["2000"], ["EUR"], ["2113.56"], ["USD"]]
estimates: [["50"], ["2500"], ["EUR"], ["505"], ["2600"], ["USD"]]

Fortunately, actual calls to the controller are parsed correctly. I need the tests fixed though obviously, so any help would be really appreciated!

Upvotes: 2

Views: 361

Answers (2)

user2992971
user2992971

Reputation: 92

Add as: :json option:

post :create, as: :json, params: {
  model: {
    prices: [[2000, "EUR"], [2113.56, "USD"]],
     estimates: [[50, 2500, "EUR"], [505, 2600, "USD"]]
  }
}

Upvotes: 0

Romanos
Romanos

Reputation: 149

Found the answer after so much time..

We thought that by having only: @request.headers['Accept'] = 'application/json' in setup was ok.

But we also had to include: @request.headers['Content-Type'] = 'application/json' in order for the params to be parsed correctly!

Upvotes: 3

Related Questions