使用Rspec测试ActiveModel :: Serializer类

gne*_*kus 30 rspec ruby-on-rails active-model-serializers ruby-on-rails-4

鉴于以下ActiveModel::Serializer课程:

class SampleSerializer < ActiveModel::Serializer
  attributes :id, :name
end
Run Code Online (Sandbox Code Playgroud)

如何测试RSpec

gne*_*kus 27

假设

这个答案假设你有rspec-rails,active_model_serializers以及factory_girl_rails安装和配置的宝石.

此答案还假设您已为Sample资源定义了工厂.

串行器规格

对于编写本文时的active_model_serializers的当前版本(0.10.0.rc3),ActiveModel::Serializer类不会接收to_json,而是包装在适配器类中.要获取包装在序列化程序实例中的模型的序列化,必须创建适配器的实例:

before(:each) do
  # Create an instance of the model
  @sample = FactoryGirl.build(:sample)

  # Create a serializer instance
  @serializer = SampleSerializer.new(@sample)

  # Create a serialization based on the configured adapter
  @serialization = ActiveModelSerializers::Adapter.create(@serializer)
end
Run Code Online (Sandbox Code Playgroud)

适配器实例接收to_json方法并返回模型的序列化.

subject { JSON.parse(@serialization.to_json) }
Run Code Online (Sandbox Code Playgroud)

然后可以在返回的JSON上运行期望.

it 'should have a name that matches' do
  expect(subject['name']).to eql(@sample.name)
end
Run Code Online (Sandbox Code Playgroud)

解析JSON响应时,必须考虑适配器配置:

  • 一个注意事项:不推荐使用ActiveModel :: Serializer :: Adapter.create.使用ActiveModelSerializers :: Adapter.create. (5认同)

Jan*_*imo 19

使用active_model_serializers时,只需调用serializable_hash序列化程序就可以轻松实现:

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


Luk*_*ler 15

@ gnerkus的回答有助于指导我自己的实现,但我选择了不同的方法.测试返回值,ActiveModel::Serializer其中没有串行器正在进行额外处理的地方似乎正在测试特定键的存在以及是否ActiveModel::Serializer正在工作.为避免测试ActiveModel::Serializer而是测试是否存在特定键,以下是我如何测试给定的Serializer:

describe SampleSerializer do
  subject {  SampleSerializer.new(sample) }

  it "includes the expected attributes" do
    expect(subject.attributes.keys).
      to contain_exactly(
        :sample_key,
        :another_sample_key
      )
  end

  def sample
    @sample ||= build(:sample)
  end
end
Run Code Online (Sandbox Code Playgroud)

请注意使用contain_exactly:这可确保不存在除指定键之外的其他键.include如果包含意外属性,则使用将导致测试不失败.当您更新属性但未能更新测试时,这会很好地扩展,因为测试会抛出错误并迫使您保持最新状态.

测试密钥的例外情况是,当您想要测试已添加到给定序列化程序的自定义方法时,在这种情况下,我强烈建议为受该方法影响的返回值编写测试.

更新

为了测试关系,您需要使用序列化器进行更多设置.我为简单的序列化程序避免了这种设置,但是这个修改后的设置将帮助您测试链接,关系等的存在.

describe SampleSerializer do
  subject do
    ActiveModelSerializers::Adapter.create(sample_serializer)
  end

  it "includes the expected attributes" do
    expect(subject_json(subject)["data"]["attributes"].keys).
      to contain_exactly(
        "date"
      )
  end

  it "includes the related Resources" do
    expect(subject_json(subject)["data"]["relationships"].keys).
      to contain_exactly(
        "other-resources"
      )
  end

  def subject_json(subject)
    JSON.parse(subject.to_json)
  end

  def sample_resource
    @sample_resource ||= build(:sample_resource)
  end

  def sample_serializer
    @sample_serializer ||=
      SampleSerializer.new(sample_resource)
  end
end
Run Code Online (Sandbox Code Playgroud)


Eng*_*san 7

示例:您可以编写此现代样式.

类别序列化器:

class CategorySerializer < ActiveModel::Serializer
  attributes :id, :name
end
Run Code Online (Sandbox Code Playgroud)

RSpec的:

require 'rails_helper'

RSpec.describe CategorySerializer, type: :serializer do
  let(:category) { FactoryGirl.build(:category) }
  let(:serializer) { described_class.new(category) }
  let(:serialization) { ActiveModelSerializers::Adapter.create(serializer) }

  let(:subject) { JSON.parse(serialization.to_json) }

  it 'has an id that matches' do
    expect(subject['id']).to eql(category.id)
  end

  it 'has a name that matches' do
    expect(subject['name']).to eql(category.name)
  end  
end
Run Code Online (Sandbox Code Playgroud)