RSpec::Matchers.define :match_response_schema do |schema|
match do |response|
if schema.include?('_')
class_name = schema.split('_')
class_name = class_name.collect(&:capitalize)
schema = class_name.join('')
end
JSON::Validator.validate!("#{schema}".safe_constantize.schema, response, strict: true)
end
end
RSpec Test
RSpec.describe BluepagesService do
describe '#Positive scenarios' do
it 'Fetch data from FakeObject' do
data = FakeService.fetch_data
expect(data).to match_response_schema('FakeObject')
end
end
end
Getting this Error
1) FakeService#Positive scenarios Fetch data from FakeObject
Failure/Error: JSON::Validator.validate!("#{schema}".safe_constantize.schema, response, strict: true)
NoMethodError:
undefined method `each' for "search":String
# /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/attributes/required.rb:12:in `validate'
# /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/schema/validator.rb:25:in `block in validate'
# /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/schema/validator.rb:23:in `each'
# /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/schema/validator.rb:23:in `validate'
# /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/schema.rb:33:in `validate'
# /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/validator.rb:113:in `validate'
# /Users/thiagogs/.rvm/gems/ruby-2.4.1/gems/json-schema-2.8.0/lib/json-schema/validator.rb:254:in `validate!'
# ./spec/support/api_schema_matcher.rb:11:in `block (2 levels) in <top (required)>'
# ./spec/services/fake_service_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 2.96 seconds (files took 2.46 seconds to load)
Guys I'm trying to write some tests using a json matcher... but without luck
JSON Object from tests
Schema to be validated
Custom Matcher
RSpec Test
Getting this Error
Versions
Gems installed
I'm not sure which one will be important on this issue, if you need more I will be happy to update it.