'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 1:[1]
For everyone wondering:
- This is caused by the
Rack::Testimplementation and notRSpec. - There is a shortcut for the Bartosz's answer:
post 'api/users/remove', params: params, as: :json
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jibiel |
