From 32638062d7a028f0d1d72ba27024e42181912a22 Mon Sep 17 00:00:00 2001 From: Jay Dorsey <191564+jaydorsey@users.noreply.github.com> Date: Fri, 6 Aug 2021 23:09:56 -0400 Subject: [PATCH 1/2] 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) --- .../lib/rswag/specs/request_factory.rb | 26 ++++++++++++++++++- .../spec/rswag/specs/request_factory_spec.rb | 15 +++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/rswag-specs/lib/rswag/specs/request_factory.rb b/rswag-specs/lib/rswag/specs/request_factory.rb index 523b50f..f7b9fce 100644 --- a/rswag-specs/lib/rswag/specs/request_factory.rb +++ b/rswag-specs/lib/rswag/specs/request_factory.rb @@ -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 diff --git a/rswag-specs/spec/rswag/specs/request_factory_spec.rb b/rswag-specs/spec/rswag/specs/request_factory_spec.rb index aff5fb4..a9faef2 100644 --- a/rswag-specs/spec/rswag/specs/request_factory_spec.rb +++ b/rswag-specs/spec/rswag/specs/request_factory_spec.rb @@ -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'] From 86c512f639758dd389769c2c1d8de2c8e93a4f99 Mon Sep 17 00:00:00 2001 From: Jay Dorsey <191564+jaydorsey@users.noreply.github.com> Date: Fri, 24 Sep 2021 16:11:44 -0400 Subject: [PATCH 2/2] Add single quotes around parameter name --- rswag-specs/lib/rswag/specs/request_factory.rb | 2 +- rswag-specs/spec/rswag/specs/request_factory_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rswag-specs/lib/rswag/specs/request_factory.rb b/rswag-specs/lib/rswag/specs/request_factory.rb index f7b9fce..eb13b2d 100644 --- a/rswag-specs/lib/rswag/specs/request_factory.rb +++ b/rswag-specs/lib/rswag/specs/request_factory.rb @@ -216,7 +216,7 @@ module Rswag def message <<~MSG - Missing parameter #{body_param} + 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: diff --git a/rswag-specs/spec/rswag/specs/request_factory_spec.rb b/rswag-specs/spec/rswag/specs/request_factory_spec.rb index a9faef2..a26f694 100644 --- a/rswag-specs/spec/rswag/specs/request_factory_spec.rb +++ b/rswag-specs/spec/rswag/specs/request_factory_spec.rb @@ -171,7 +171,7 @@ module Rswag 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.to raise_error(Rswag::Specs::MissingParameterError, /Missing parameter 'comment'/) end end