2017-08-04 09:45:14 -04:00
|
|
|
module SchemaPath
|
|
|
|
def self.expand(schema, dir = '')
|
|
|
|
Rails.root.join('spec', dir, "fixtures/api/schemas/#{schema}.json").to_s
|
|
|
|
end
|
2017-06-06 13:35:28 -04:00
|
|
|
end
|
|
|
|
|
2017-08-04 09:45:14 -04:00
|
|
|
RSpec::Matchers.define :match_response_schema do |schema, dir: '', **options|
|
2016-08-01 14:41:30 -04:00
|
|
|
match do |response|
|
2017-08-04 09:45:14 -04:00
|
|
|
@errors = JSON::Validator.fully_validate(
|
|
|
|
SchemaPath.expand(schema, dir), response.body, options)
|
2017-07-26 08:33:09 -04:00
|
|
|
|
|
|
|
@errors.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |response|
|
2017-08-04 09:45:14 -04:00
|
|
|
"didn't match the schema defined by #{SchemaPath.expand(schema, dir)}" \
|
2017-07-26 08:33:09 -04:00
|
|
|
" The validation errors were:\n#{@errors.join("\n")}"
|
2017-06-06 13:35:28 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-01 14:41:30 -04:00
|
|
|
|
2017-08-04 09:45:14 -04:00
|
|
|
RSpec::Matchers.define :match_schema do |schema, dir: '', **options|
|
2017-06-06 13:35:28 -04:00
|
|
|
match do |data|
|
2017-08-04 09:45:14 -04:00
|
|
|
JSON::Validator.validate!(SchemaPath.expand(schema, dir), data, options)
|
2016-08-01 14:41:30 -04:00
|
|
|
end
|
|
|
|
end
|