简体   繁体   English

如何修复“路径“名称”上的json原子丢失”?

[英]How to fix 'json atom at path “name” is missing'?

I'm doing a serializer for my application and i need to test it, but it always return 'json atom at path "name" is missing' when i run the specs. 我正在为我的应用程序做一个序列化程序,我需要对其进行测试,但是在运行规格时,它总是返回“路径“名称”丢失的json原子”。 I'm using FastJson do build my serializer. 我正在使用FastJson来构建我的序列化程序。

My StudentSerializer (all the attributes is in Students Model): 我的StudentSerializer(所有属性都在Students Model中):

# frozen_string_literal: true

class Api::V2::StudentSerializer < ApplicationSerializer
  attributes :email, :name, :school_id
end 

My StudentSerializer_Spec: 我的StudentSerializer_Spec:

require 'rails_helper'

RSpec.describe Api::V2::StudentSerializer, type: :serializer do

  subject(:serializer) { described_class.new(student) }

  context 'is student serialize working?' do
    let(:student) { build_stubbed(:student) }

    it 'serializes' do
      Api::V2:: StudentSerializer.new (student)
      expect(serializer).to include_json(
        name: student.name
      )
    end
  end
end

When i run the rspec, that's the result i get: 当我运行rspec时,得到的结果是:

Api::V2::StudentSerializer
  is student serialize working?
    serializes (FAILED - 1)

  1) Api::V2::StudentSerializer is student serialize working? serializes
     Failure/Error:
       expect(serializer).to include_json(
         name: student.name
       )

                 json atom at path "name" is missing

I figure it out! 我知道了!

By using FastJson, the Json start with 'data:': 通过使用FastJson,Json以'data:'开头:

{:data=>
  {:id=>"1",
   :attributes=>
    {:email=>"",
     :name=>"Burdette Schmeler",
     :school_id=>1}}}

So, in my spec file, i need to put in this way: 因此,在我的规格文件中,我需要这样写:

  context 'is student serialize working?' do
    let(:student) { create(:student) }

    it 'serializes' do
      expect(serializer).to include_json(
        data: {
          id: student.id.to_s,
          attributes: {
            name: student.name,
            email: student.email,
            school_id: student.school_id,
          }
        }
      )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM