How to write unit test using rspec for serializer with custom attributes

家住魔仙堡 提交于 2019-12-11 06:17:22

问题


Hi i am working on a RoR project with ruby-2.5.0 and rails 5. I am using the jsonapi-serializers for my api. I have a serializer with custom attribute as follows:-

class ReceiptPartialSerializer
  include JSONAPI::Serializer

  TYPE = 'receipt'
  attribute :id
  attribute :receipt_partials

  attribute :receipt_partials do
    receipt_container = []
    object.receipt_partials.each do |partial|
      receipt_partial = {}
      receipt_partial['id'] = partial.id
      receipt_partial['image_url'] = 'https:' + partial&.picture&.url
      receipt_container << receipt_partial
    end
    receipt_container
  end
end

My spec file for this serializer is:-

RSpec.describe ReceiptPartialSerializer do
  let(:id) { 1 }
  let(:image_url) { 'https:/images/original/missing.png' }
  let(:receipt_id) { 1 }
  let(:receipt_partial) do
    ReceiptPartial.new(
      receipt_id: receipt_id
    )
  end

  subject { JSONAPI::Serializer.serialize(receipt_partial) }

  it { is_expected.to have_jsonapi_attributes('image-url' => image_url) }
end

But when i run my test it doesnot cover my custom attribute which is receipt_partials. Any idea how can i cover my custom attribute. Thanks in advance.


回答1:


It looks like you're only testing one attribute of your serializer - which is image-url. Have you tried adding an expectation for what is returned by receipt_partial? You could store receipt partial in a method in your test by using something like so:

def receipt_partial
  @receipt_partial ||= ReceiptPartial.new(receipt_id: receipt_id)
end

And then update your test to look like this:

describe ReceiptPartialSerializer do
  let(:id) { 1 }
  let(:image_url) { 'https:/images/original/missing.png' }
  let(:receipt_id) { 1 }
  let(:receipt_partial) { receipt_partial }

  subject { JSONAPI::Serializer.serialize(receipt_partial) }

  it { is_expected.to have_jsonapi_attributes('image-url' => image_url) }
  it { is_expected.to have_jsonapi_attributes('receipt_partial' => receipt_partial) }

  def receipt_partial
    @receipt_partial ||= ReceiptPartial.new(receipt_id: receipt_id)
  end
end

You'll want to check if have_jsonapi_attributes accepts an array, and if it does, you could consolidate that expectation statement into one large expectation of all of your serializer's arguments.

Also, per my answer here, I believe serializers should be tested with explicit expectations for what is returned, instead of testing whether a given attribute is included. This is my first time seeing the have_jsonapi_attributes helper, so if that's the way that works, great. Otherwise, it might be worth adjusting your test for explicit expectations for what is returned.




回答2:


@awsm-sid Did you debugging of the code ?



来源:https://stackoverflow.com/questions/50781617/how-to-write-unit-test-using-rspec-for-serializer-with-custom-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!