为什么我得到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"
    }
  }
}

请注意,在“项目”,“作者”或“属性”之后都没有结尾的逗号。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...