如何解决为什么我得到SON :: ParserError尝试使用json_matchers验证响应?
app / models / author.rb:
class Author < ApplicationRecord
validates :name,presence: true,uniqueness: { case_sensitive: false }
end
app / serializers / author_serializer.rb:
class AuthorSerializer < ActiveModel::Serializer
attributes :id,:name,:bio
end
spec / spec_helper.rb:
#...
require 'json_matchers/rspec'
JsonMatchers.schema_root = 'spec/support/api/schemas.author.json':
#...
spec / support / api / schemas / authors / show.json:
{
"id": "file:/authors/show.json#","type": "object","definitions": {
"authors": {
"description": "A collection of authors","example": [{ "id": "1","name": "Name" }],"type": "array","items": {
"$ref": "file:/author.json#"
}
}
},"required": ["authors"],"properties": {
"authors": {
"$ref": "#/definitions/authors"
}
}
}
spec / requests / authors_show_request_pec.rb:
RSpec.describe 'Authors',type: :request do
setup { host! 'api.example.com' }
describe 'GET /author/:id' do
let!(:author) { create(:author,id: 13,name: 'Name',bio: 'bio') }
it 'returns requested author' do
get author_path(13)
expect(response).to have_http_status(200)
author_from_response = JSON.parse(response.body)
expect(author_from_response['name']).to eq(author.name)
expect(author_from_response['bio']).to eq(author.bio)
expect(response).to match_json_schema('author')
end
end
结束
响应主体包含所有预期数据,但指定falinig是否验证对JSON模式的匹配响应。 Json_matchers gem似乎是根据手册配置的。 出现的错误:
JsonMatchers::InvalidSchemaError:
783: unexpected token at '}
'
解决方法
尝试删除结尾的逗号。 ruby JSON解析器不喜欢它们。 您的JSON响应应为:
{
"id": "file:/authors/index.json#","type": "object","definitions": {
"authors": {
"description": "A collection of authors","example": [{ "id": "1","name": "Name" }],"type": "array","items": {
"$ref": "file:/author.json#"
}
}
},"required": ["authors"],"properties": {
"authors": {
"$ref": "#/definitions/authors"
}
}
}
请注意,在“项目”,“作者”或“属性”之后都没有结尾的逗号。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。