Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: swagger schema rspec matcher #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion lib/apress/api/testing/json_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,47 @@
options = {strict: true}.merge!(options || {})
JSON::Validator.validate!(schema, json, options)
end
end
end

# Defines rspec matcher to check expected json matches swagger model schema
#
# Examples
#
# it 'expect response body is similar to swagger doc' do
# expect(response.body).to match_swagger_schema('Swagger::V1::YourCustomSchema')
# end
RSpec::Matchers.define :match_swagger_schema do |expected|
match do |actual|
schema = swagger_schema[:definitions][swagger_model_name]
raise "Swagger schema #{swagger_model_name} does not exists" if schema.nil?

match_json_schema(schema).match(expected)
end

description do
"matches swagger documentation schema #{swagger_model_name}"
end

failure_message do |actual|
"expected #{actual} to match swagger schema #{swagger_model_name}"
end

failure_message_when_negated do |actual|
"expected #{actual} not to match swagger schema #{swagger_model_name}"
end

def swagger_schema
@swagger_schema ||= Apress::Api::Swagger::Generator.new.data
end

def swagger_model_name
case expected
when Class
expected.name.to_sym
when String
expected.to_sym
else
expected
end
end
end