Skip to content

Instantly share code, notes, and snippets.

@milgner milgner/api.rb
Created Mar 27, 2018

Embed
What would you like to do?
Grape shared params error
# frozen_string_literal: true
require 'grape'
class API < Grape::API
helpers do
params :shared_params do
optional :shared1, type: String
optional :shared2, type: String
exactly_one_of :shared1, :shared2
end
end
params do
requires :MessageID, type: String, values: %w[foo bar baz]
given MessageID: ->(val) { val == 'foo' } do
requires :foo, type: String
end
given MessageID: ->(val) { val == 'bar' } do
use :shared_params
requires :bar, type: String
end
given MessageID: ->(val) { val == 'EntryNotification' } do
use :shared_params
requires :baz, type: String
end
end
post '/' do
end
end
# frozen_string_literal: true
require 'spec_helper'
require 'api'
describe API do
include Rack::Test::Methods
def app
API
end
it "doesn't require 'shared' for message id 'foo'" do
post '/', { MessageID: 'foo', foo: 'foo' }.to_json, 'CONTENT_TYPE' => 'application/json'
puts last_response.body
expect(last_response.status).to eq(201)
end
end
➜ rspec
shared1, shared2 are missing, exactly one parameter must be provided
F
Failures:
1) API doesn't require 'shared' for message id 'foo'
Failure/Error: expect(last_response.status).to eq(201)
expected: 201
got: 400
(compared using ==)
# ./spec/api_spec.rb:16:in `block (2 levels) in <top (required)>'
Finished in 0.03982 seconds (files took 0.20358 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/api_spec.rb:13 # API doesn't require 'shared' for message id 'foo'
@milgner

This comment has been minimized.

Copy link
Owner Author

commented Mar 27, 2018

Here's a zip of the full directory: https://oc.illunis.net/index.php/s/ejXzHAYkCcojcCw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.