awsm sid
awsm sid

Reputation: 595

How to write unit test for serializer using rspec

Hi i am working on a RoR project with ruby-2.5.0 and rails 5. I have two models Receipt and Receipt Items. Receipt has_many receipt_items. Receipt Serializer:-

# frozen_string_literal: true

class ReceiptSerializer
  include JSONAPI::Serializer

  TYPE = 'reciept'
  attribute :name
  attribute :receipt_date
  attribute :address
  attribute :total_paid
  attribute :user_id

  attribute :receipt_date do
    object.receipt_date.strftime('%d/%m/%Y %H:%M:%S')
  end

  attribute :receipt_items do 
    object.receipt_items.map do |receipt_item|
      ::ReceiptItemSerializer.new(receipt_item).attributes
    end
  end
end

Receipt Items Serializer:-

# frozen_string_literal: true

class ReceiptItemSerializer
  include JSONAPI::Serializer

  TYPE = 'reciept_item'
  attribute :item_name
  attribute :quantity
  attribute :price

end

I have written the unit test for both the serializers as follows:- receipt_serializer_spec.rb

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ReceiptSerializer do
  let(:id) { 1 }
  let(:user_id) { 1 }
  let(:name) { 'IGA' }
  let(:address) { 'address' }
  let(:total_paid) { '100' }
  let(:receipt_date) { '12/04/2018 15:36:00' }
  let(:receipt) do
    Receipt.new(
      user_id: user_id,
      name: name,
      address: address,
      total_paid: total_paid,
      receipt_date: receipt_date
    )
  end

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

  it { is_expected.to have_jsonapi_attributes('user-id' => user_id) }

  it { is_expected.to have_jsonapi_attributes('address' => address) }

  it { is_expected.to have_jsonapi_attributes('total-paid' => total_paid) }

  it { is_expected.to have_jsonapi_attributes('receipt-date' => receipt_date) }

  it { is_expected.to have_jsonapi_attributes('receipt-date' => receipt_date) }
end

receipt_item_serializer_spec.rb

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ReceiptSerializer do
  let(:receipt_id) { 1 }
  let(:item_name) { 'ABC' }
  let(:quantity) { 1 }
  let(:price) { '100' }
  let(:receipt_item) do
    ReceiptItem.new(
      receipt_id: receipt_id,
      item_name: item_name,
      quantity: quantity,
      price: price
    )
  end

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

  it { is_expected.to have_jsonapi_attributes('item-name' => item_name) }

  it { is_expected.to have_jsonapi_attributes('quantity' => quantity) }

  it { is_expected.to have_jsonapi_attributes('price' => price) }
end

Now i don't know how to write unit test for receipt_items attribute which i have defined in the receipt_serializer.rb. Please help me. Thanks in advance.

Upvotes: 2

Views: 1562

Answers (2)

Stanislav Kr.
Stanislav Kr.

Reputation: 554

I hope this gem will be useful for you https://github.com/collectiveidea/json_spec

Upvotes: 1

Paul Byrne
Paul Byrne

Reputation: 1615

You could write

it "serializes the receipt" do
  expect(subject).to include('item-name' => 'ABC', 'item-price' => '100') # ... etc
end

Not sure there's huge value to testing this, if you're individually testing the presence of all your attributes already.

Upvotes: 1

Related Questions