No commit activity in last 3 years
No release in over 3 years
RSpec Matchers for ActiveModel::Serializer Associations
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0

Runtime

 Project Readme

ActiveModelSerializersMatchers

Gem Version Travis CI Coverage Status Code Climate

RSpec Matchers for ActiveModel::Serializer Associations

Note: This gem requires "active_model_serializers", "~> 0.8.0":

Installation

Add this line to your application's Gemfile:

gem 'active_model_serializers_matchers', '0.2.1'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_model_serializers_matchers

Configure RSpec

RSpec.configure do |config|
  config.include ActiveModelSerializersMatchers, :type => :serializer
end

Usage

Associations

has_one and has_many associations

association matcher: #have_one and #have_many

class ListSerializer < ActiveModel::Serializer
  has_one :title
  has_many :items
end

RSpec.describe ListSerializer, :type => :serializer do
  subject { described_class }
  it { should have_one(:title) }
  it { should have_many(:items) }
  it { should have_many(:cats) }
end

# ListSerializer
#   should have one :title
#   should have many :items
#   should have many :cats (FAILED - 1)

key option

option matcher: #as

class ShoeRackSerializer < ActiveModel::Serializer
  has_many :shoes, key: :kicks
end

RSpec.describe ShoeRackSerializer, :type => :serializer do
  subject { described_class }
  it { should have_many(:shoes).as(:kicks) }
  it { should have_many(:shoes).as(:ones_and_twos) }
end

# ShoeRackSerializer
#   should have many :shoes as :kicks
#   should have many :shoes as :ones_and_twos (FAILED - 1)

serializer option

option matcher: #serialized_with

class ProductSerializer < ActiveModel::Serializer; end
class SoupCanSerializer < ActiveModel::Serializer; end

class ShoppingCartSerializer < ActiveModel::Serializer
  has_many :items, serializer: ProductSerializer
end

RSpec.describe ShoppingCartSerializer, :type => :serializer do
  subject { described_class }
  it { should have_many(:items).serialized_with(ProductSerializer) }
  it { should have_many(:items).serialized_with(SoupCanSerializer) }
end

# ShoppingCartSerializer
#   should have many :items serialized with ProductSerializer
#   should have many :items serialized with SoupCanSerializer (FAILED - 1)

chaining multiple matchers

Multiple option matchers can be chained onto an association matcher in any order.

class FoodSerializer < ActiveModel::Serializer; end

class MenuSerializer < ActiveModel::Serializer
  has_many :entrees, key: :dishes, serializer: FoodSerializer
end

RSpec.describe MenuSerializer, :type => :serializer do
  subject { described_class }
  it { should have_many(:entrees).as(:dishes).serialized_with(FoodSerializer) }
  it { should have_many(:entrees).serialized_with(FoodSerializer).as(:dishes) }
end

# MenuSerializer
#   should have many :entrees as :dishes serialized with FoodSerializer
#   should have many :entrees serialized with FoodSerializer as :dishes

Contributing

  1. Fork it ( https://github.com/tonyta/active_model_serializers_matchers/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request