debian-mirror-gitlab/spec/support/api/schema_matcher.rb

26 lines
729 B
Ruby
Raw Normal View History

2017-09-10 17:25:29 +05:30
module SchemaPath
def self.expand(schema, dir = '')
Rails.root.join('spec', dir, "fixtures/api/schemas/#{schema}.json").to_s
end
end
RSpec::Matchers.define :match_response_schema do |schema, dir: '', **options|
2016-09-13 17:45:13 +05:30
match do |response|
2017-09-10 17:25:29 +05:30
@errors = JSON::Validator.fully_validate(
SchemaPath.expand(schema, dir), response.body, options)
@errors.empty?
end
failure_message do |response|
"didn't match the schema defined by #{SchemaPath.expand(schema, dir)}" \
" The validation errors were:\n#{@errors.join("\n")}"
end
end
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
RSpec::Matchers.define :match_schema do |schema, dir: '', **options|
match do |data|
JSON::Validator.validate!(SchemaPath.expand(schema, dir), data, options)
2016-09-13 17:45:13 +05:30
end
end