arraysrubyrspecruby-grape

Rspec interprets empty array as lack of parameter with Grape


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).


Solution

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