mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-24 06:46:42 +00:00
Better error message for missing let
I've run into this problem a number of times. The original error message is actually a `NoMethodError` and it's not always immediately clear what the solution is, particuarly if you're new to rspec or rswag Wanted to see if this kind of behavior is something that the rswag team would be interested in adopting. I have a few other of these in mind (will do as small PRs, with tests)
This commit is contained in:
parent
095906da58
commit
32638062d7
@ -194,12 +194,36 @@ module Rswag
|
||||
|
||||
def build_json_payload(parameters, example)
|
||||
body_param = parameters.select { |p| p[:in] == :body }.first
|
||||
body_param ? example.send(body_param[:name]).to_json : nil
|
||||
|
||||
return nil unless body_param
|
||||
|
||||
raise(MissingParameterError, body_param[:name]) unless example.respond_to?(body_param[:name])
|
||||
|
||||
example.send(body_param[:name]).to_json
|
||||
end
|
||||
|
||||
def doc_version(doc)
|
||||
doc[:openapi] || doc[:swagger] || '3'
|
||||
end
|
||||
end
|
||||
|
||||
class MissingParameterError < StandardError
|
||||
attr_reader :body_param
|
||||
|
||||
def initialize(body_param)
|
||||
@body_param = body_param
|
||||
end
|
||||
|
||||
def message
|
||||
<<~MSG
|
||||
Missing parameter #{body_param}
|
||||
|
||||
Please check your spec. It looks like you defined a body parameter,
|
||||
but did not declare usage via let. Try adding:
|
||||
|
||||
let(:#{body_param}) {}
|
||||
MSG
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -160,6 +160,21 @@ module Rswag
|
||||
end
|
||||
end
|
||||
|
||||
context 'missing body parameter' do
|
||||
before do
|
||||
metadata[:operation][:parameters] = [{ name: 'comment', in: :body, schema: { type: 'object' } }]
|
||||
allow(example).to receive(:comment).and_raise(NoMethodError, "undefined method 'comment'")
|
||||
allow(example).to receive(:respond_to?).with(:'Content-Type')
|
||||
allow(example).to receive(:respond_to?).with('comment').and_return(false)
|
||||
end
|
||||
|
||||
it 'uses the referenced metadata to build the request' do
|
||||
expect do
|
||||
request[:payload]
|
||||
end.to raise_error(Rswag::Specs::MissingParameterError, /Missing parameter comment/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'form payload' do
|
||||
before do
|
||||
metadata[:operation][:consumes] = ['multipart/form-data']
|
||||
|
||||
Loading…
Reference in New Issue
Block a user