Skip to content

Commit

Permalink
Add belongs_to matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
josephchoe committed Sep 16, 2017
1 parent f8556ae commit 9bbcee1
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/active_model_serializers_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Dir[Pathname(__FILE__).join('../**/*.rb')].each { |f| require f }

module ActiveModelSerializersMatchers
def belong_to(association_root)
AssociationMatcher.new(association_root, :belongs_to)
end

def have_many(association_root)
AssociationMatcher.new(association_root, :has_many)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def failure_message

def association_string
case type
when :belongs_to
'belong to'
when :has_one
'have one'
when :has_many
Expand All @@ -38,6 +40,8 @@ def association_string

def association_reflection
case type
when :belongs_to
ActiveModel::Serializer::BelongsToReflection
when :has_one
ActiveModel::Serializer::HasOneReflection
when :has_many
Expand Down
109 changes: 109 additions & 0 deletions spec/active_model_serializers_matchers/association_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,113 @@
end
end
end

###

context 'when given serializer that belongs_to :foo' do
subject do
Class.new(ActiveModel::Serializer) { belongs_to :foo }
end
it_behaves_like 'it belongs_to :foo'
it_behaves_like 'it belongs_to :foo and no key'
it_behaves_like 'it belongs_to :foo and no serializer'
it_behaves_like 'it belongs_to :foo and no embed_key'
end

context 'when given serializer that belongs_to :foo, key: :bar' do
subject do
Class.new(ActiveModel::Serializer) { belongs_to :foo, key: :bar }
end
it_behaves_like 'it belongs_to :foo'
it_behaves_like 'it belongs_to :foo and no serializer'
it_behaves_like 'it belongs_to :foo and no embed_key'
it_behaves_like 'it belongs_to :foo with key :bar'
end

context 'when given serializer that belongs_to :foo, serializer: FooSerializer' do
subject do
Class.new(ActiveModel::Serializer) { belongs_to :foo, serializer: FooSerializer }
end
it_behaves_like 'it belongs_to :foo'
it_behaves_like 'it belongs_to :foo and no key'
it_behaves_like 'it belongs_to :foo and no embed_key'
it_behaves_like 'it belongs_to :foo with serializer FooSerializer'
end

context 'when given serializer that belongs_to :foo, embed_key: :foo_external_id' do
subject do
Class.new(ActiveModel::Serializer) { belongs_to :foo, embed_key: :foo_external_id }
end
it_behaves_like 'it belongs_to :foo'
it_behaves_like 'it belongs_to :foo and no key'
it_behaves_like 'it belongs_to :foo and no serializer'
it_behaves_like 'it belongs_to :foo with embed_key :foo_external_id'
end

context 'when given serializer that belongs_to :foo, key: :bar, serializer: FooSerializer' do
subject do
Class.new(ActiveModel::Serializer) { belongs_to :foo, key: :bar, serializer: FooSerializer }
end
it_behaves_like 'it belongs_to :foo'
it_behaves_like 'it belongs_to :foo with key :bar'
it_behaves_like 'it belongs_to :foo with serializer FooSerializer'

describe 'should if all chained expectations pass in any order' do
it 'should pass a belong_to :foo as :bar serialized_with FooSerializer expectation' do
expectation = belong_to(:foo).as(:bar).serialized_with(FooSerializer)
expect(subject).to expectation
expect(expectation.description).to eq('belong to :foo as :bar serialized with FooSerializer')
end

it 'should pass a belong_to :foo serialized_with FooSerializer as :bar expectation' do
expectation = belong_to(:foo).serialized_with(FooSerializer).as(:bar)
expect(subject).to expectation
expect(expectation.description).to eq('belong to :foo serialized with FooSerializer as :bar')
end
end

describe 'should fail with the first encountered expectation message if any chained expectations fail' do
it 'should fail a belong_to :fuu expectation' do
failure_message = "expected #{ subject } to define a 'belongs_to :fuu' association"
expect {
expect(subject).to belong_to(:fuu).as(:bar).serialized_with(FooSerializer)
}.to fail_with(failure_message)
expect {
expect(subject).to belong_to(:fuu).serialized_with(FooSerializer).as(:bar)
}.to fail_with(failure_message)
expect {
expect(subject).to belong_to(:fuu).as(:bor).serialized_with(BarSerializer)
}.to fail_with(failure_message)
expect {
expect(subject).to belong_to(:fuu).serialized_with(BarSerializer).as(:bor)
}.to fail_with(failure_message)
end

it 'should fail a belong_to :foo serialized with BarSerializer expectation' do
failure_message = "expected #{ subject } 'belongs_to :foo' association to explicitly have serializer BarSerializer but instead was FooSerializer"
expect {
expect(subject).to belong_to(:foo).as(:bar).serialized_with(BarSerializer)
}.to fail_with(failure_message)
expect {
expect(subject).to belong_to(:foo).serialized_with(BarSerializer).as(:bar)
}.to fail_with(failure_message)
expect {
expect(subject).to belong_to(:foo).serialized_with(BarSerializer).as(:bor)
}.to fail_with(failure_message)
end

it 'should fail a belong_to :foo as :bor expectation' do
failure_message = "expected #{ subject } 'belongs_to :foo' association to explicitly have key :bor but instead was :bar"
expect {
expect(subject).to belong_to(:foo).serialized_with(FooSerializer).as(:bor)
}.to fail_with(failure_message)
expect {
expect(subject).to belong_to(:foo).as(:bor).serialized_with(FooSerializer)
}.to fail_with(failure_message)
expect {
expect(subject).to belong_to(:foo).as(:bor).serialized_with(BarSerializer)
}.to fail_with(failure_message)
end
end
end
end
9 changes: 9 additions & 0 deletions spec/active_model_serializers_matchers_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require 'spec_helper'

describe ActiveModelSerializersMatchers do
describe '#belong_to' do
it 'returns a new AssociationMatcher of type :belongs_to' do
expect(described_class::AssociationMatcher)
.to receive(:new).with(:association_root, :belongs_to)
.and_call_original
expect(belong_to(:association_root))
.to be_a described_class::AssociationMatcher
end
end

describe '#have_many' do
it 'returns a new AssociationMatcher of type :has_many' do
Expand Down
80 changes: 80 additions & 0 deletions spec/support/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,83 @@
}.to fail_with("expected #{ subject } 'has_many :foos' association to explicitly have embed key :bar_external_id but instead was :foo_external_id")
end
end

shared_examples 'it belongs_to :foo' do
it 'should pass a belong_to :foo expectation' do
expectation = belong_to(:foo)
expect(subject).to expectation
expect(expectation.description).to eq('belong to :foo')
end

it 'should fail a belong_to :fuu expectation' do
expect {
expect(subject).to belong_to(:fuu)
}.to fail_with("expected #{ subject } to define a 'belongs_to :fuu' association")
end
end

shared_examples 'it belongs_to :foo and no key' do
it 'should fail a belong_to :foo as :bar expectation' do
expect {
expect(subject).to belong_to(:foo).as(:bar)
}.to fail_with("expected #{ subject } 'belongs_to :foo' association to explicitly have key :bar but instead has none")
end
end

shared_examples 'it belongs_to :foo and no serializer' do
it 'should fail a belong_to :foo serialized with FooSerializer expectation' do
expect {
expect(subject).to belong_to(:foo).serialized_with(FooSerializer)
}.to fail_with("expected #{ subject } 'belongs_to :foo' association to explicitly have serializer FooSerializer but instead has none")
end
end

shared_examples 'it belongs_to :foo and no embed_key' do
it 'should fail a belong_to :foo with_embed_key :foo_external_id expectation' do
expect {
expect(subject).to belong_to(:foo).with_embed_key(:foo_external_id)
}.to fail_with("expected #{ subject } 'belongs_to :foo' association to explicitly have embed key :foo_external_id but instead has none")
end
end

shared_examples 'it belongs_to :foo with key :bar' do
it 'should pass a belong_to :foo as :bar expectation' do
expectation = belong_to(:foo).as(:bar)
expect(subject).to expectation
expect(expectation.description).to eq('belong to :foo as :bar')
end

it 'should fail a belong_to :foo as :bor expectation' do
expect {
expect(subject).to belong_to(:foo).as(:bor)
}.to fail_with("expected #{ subject } 'belongs_to :foo' association to explicitly have key :bor but instead was :bar")
end
end

shared_examples 'it belongs_to :foo with serializer FooSerializer' do
it 'should pass a belong_to :foo as :bar expectation' do
expectation = belong_to(:foo).serialized_with(FooSerializer)
expect(subject).to expectation
expect(expectation.description).to eq('belong to :foo serialized with FooSerializer')
end

it 'should fail a belong_to :foo serialized with FooSerializer expectation' do
expect {
expect(subject).to belong_to(:foo).serialized_with(BarSerializer)
}.to fail_with("expected #{ subject } 'belongs_to :foo' association to explicitly have serializer BarSerializer but instead was FooSerializer")
end
end

shared_examples 'it belongs_to :foo with embed_key :foo_external_id' do
it 'should pass a belong_to :foo with embed key :foo_external_id' do
expectation = belong_to(:foo).with_embed_key(:foo_external_id)
expect(subject).to expectation
expect(expectation.description).to eq('belong to :foo with embed key :foo_external_id')
end

it 'should fail a belong_to :foo with embed key :bar_external_id expectation' do
expect {
expect(subject).to belong_to(:foo).with_embed_key(:bar_external_id)
}.to fail_with("expected #{ subject } 'belongs_to :foo' association to explicitly have embed key :bar_external_id but instead was :foo_external_id")
end
end

0 comments on commit 9bbcee1

Please sign in to comment.