3

I have a Ruby (non-Rails) app that uses Grape to expose API endpoints. One of the endpoints requires a parameter that is an array of values, but accepts an empty array as well:

requires :user_ids, type: Array, allow_blank: true

This all works fine when testing the endpoint manually using Curl or Postman - and empty array is properly interpreted as parameter user_ids: []. However, rspec seems to omit this whole parameter when its value is an empty array (non-empty array works perfectly of course):

let(:params) { { user_ids: [] } }
let(:route) { post "api/users/remove", params }

In this case, params that actually get passed equal {} and Grape's requires guard kicks in, not allowing the endpoint to execute anything.

Not sure if it's a bug or a feature and how to force rspec to pass this empty array as a parameter (behaves like this with both rspec 3.4 and 3.6).

Sandworm
  • 131
  • 8

2 Answers2

4

Use params.to_json and set header 'CONTENT_TYPE' to 'application/json'

  • Hi, this worked for me. For a different issue. I had an undocumented array in an api endpoint. Sending an empty array without calling `to_json` on the params made the array on the other side: `[""]` which was very weird. Why does adding these two things make this work? – DickieBoy Jul 30 '20 at 13:59
0

For everyone wondering:

  1. This is caused by the Rack::Test implementation and not RSpec.
  2. There is a shortcut for the Bartosz's answer:
post 'api/users/remove', params: params, as: :json
jibiel
  • 8,175
  • 7
  • 51
  • 74