Test ActiveModel::Serializer classes with Rspec

前端 未结 4 1935
故里飘歌
故里飘歌 2020-12-24 11:45

Given the following ActiveModel::Serializer class:

class SampleSerializer < ActiveModel::Serializer
  attributes :id, :name
end
4条回答
  •  盖世英雄少女心
    2020-12-24 12:16

    When using active_model_serializers, there is a much easier way by simply calling serializable_hash on the serializer:

    it 'should include a correct name' do
      sample = FactoryBot.create(:sample)
      serializer = SampleSerializer.new(sample)
      expect(serializer.serializable_hash[:name]).to eq 'Heisenberg'
    end
    

提交回复
热议问题