active_model_serializers_matchers 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +58 -3
- data/lib/active_model_serializers_matchers.rb +5 -0
- data/lib/active_model_serializers_matchers/have_many_association_matcher.rb +13 -13
- data/lib/active_model_serializers_matchers/have_one_association_matcher.rb +83 -0
- data/lib/active_model_serializers_matchers/version.rb +1 -1
- data/spec/active_model_serializers_matchers/have_many_association_matcher_spec.rb +17 -10
- data/spec/active_model_serializers_matchers/have_one_association_matcher_spec.rb +137 -0
- data/spec/active_model_serializers_matchers_spec.rb +8 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f077a6f2b1a3a40b7ee1b140eb7bcc717a345efd
|
4
|
+
data.tar.gz: ecd3e42fe80ca954a55d2b6de3d7faad34dd9709
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8234d7c88ae9b5d0075a5399df1fbf38b7d54020b77de42b9fb64547f00888c8294db376956ce31cc8b9b4ab37a10ad536d2ff81f48f7a09fc7632bd58155c2a
|
7
|
+
data.tar.gz: 05f2dcd60af24ee521703f6c25f9e11ec1e7d40f114955b013530f419c00140550f77f33524fa0e7020e5bfade3f4910a8737940ce8a7efd4c840529b583f6c8
|
data/README.md
CHANGED
@@ -7,8 +7,7 @@ RSpec matchers for testing ActiveModel::Serializer
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'active_model_serializers_matchers', '0.0.
|
11
|
-
git: 'git://github.com/tonyta/active_model_serializers_matchers.git'
|
10
|
+
gem 'active_model_serializers_matchers', '0.0.3'
|
12
11
|
```
|
13
12
|
|
14
13
|
And then execute:
|
@@ -20,8 +19,64 @@ Or install it yourself as:
|
|
20
19
|
$ gem install active_model_serializers_matchers
|
21
20
|
|
22
21
|
## Usage
|
22
|
+
### Simple `has_many` and `has_one` Associations
|
23
|
+
``` ruby
|
24
|
+
class ListSerializer < ActiveModel::Serializer
|
25
|
+
has_one :title
|
26
|
+
has_many :items
|
27
|
+
end
|
23
28
|
|
24
|
-
|
29
|
+
RSpec.describe ListSerializer do
|
30
|
+
it { should have_one(:title) }
|
31
|
+
it { should have_many(:items) }
|
32
|
+
end
|
33
|
+
|
34
|
+
#=> should have one title
|
35
|
+
#=> should have many items
|
36
|
+
```
|
37
|
+
|
38
|
+
### Association Options
|
39
|
+
#### Key
|
40
|
+
use: `#as`
|
41
|
+
``` ruby
|
42
|
+
class ShoeRackSerializer < ActiveModel::Serializer
|
43
|
+
has_many :shoes, key: :kicks
|
44
|
+
end
|
45
|
+
|
46
|
+
RSpec.describe ShoeRackSerializer do
|
47
|
+
it { should have_many(:shoes).as(:kicks) }
|
48
|
+
end
|
49
|
+
|
50
|
+
#=> should have many shoes as "kicks"
|
51
|
+
```
|
52
|
+
#### Serializer
|
53
|
+
use: `#serialized_with`
|
54
|
+
``` ruby
|
55
|
+
class ShoppingCartSerializer < ActiveModel::Serializer
|
56
|
+
has_many :items, serializer: ProductSerializer
|
57
|
+
end
|
58
|
+
|
59
|
+
RSpec.describe ShoppingCartSerializer do
|
60
|
+
it { should have_many(:items).serialized_with(ProductSerializer) }
|
61
|
+
end
|
62
|
+
|
63
|
+
#=> should have many items serialized with ProductSerializer
|
64
|
+
```
|
65
|
+
#### Chainable
|
66
|
+
These can be chained in any order.
|
67
|
+
``` ruby
|
68
|
+
class MenuSerializer < ActiveModel::Serializer
|
69
|
+
has_many :entrees, key: dishes, serializer: FoodSerializer
|
70
|
+
end
|
71
|
+
|
72
|
+
RSpec.describe MenuSerializer do
|
73
|
+
it { should have_many(:entrees).as(:dishes).serialized_with(FoodSerializer) }
|
74
|
+
it { should have_many(:entrees).serialized_with(FoodSerializer).as(:dishes) }
|
75
|
+
end
|
76
|
+
|
77
|
+
#=> should have many entrees as "dishes" serialized with FoodSerializer
|
78
|
+
#=> should have many entrees as "dishes" serialized with FoodSerializer
|
79
|
+
```
|
25
80
|
|
26
81
|
## Contributing
|
27
82
|
|
@@ -2,6 +2,7 @@ require "active_support"
|
|
2
2
|
|
3
3
|
require "active_model_serializers_matchers/version"
|
4
4
|
require "active_model_serializers_matchers/have_many_association_matcher"
|
5
|
+
require "active_model_serializers_matchers/have_one_association_matcher"
|
5
6
|
|
6
7
|
module ActiveModelSerializersMatchers
|
7
8
|
extend ActiveSupport::Concern
|
@@ -13,4 +14,8 @@ module ActiveModelSerializersMatchers
|
|
13
14
|
def have_many(association_root)
|
14
15
|
HaveManyAssociationMatcher.new(association_root)
|
15
16
|
end
|
17
|
+
|
18
|
+
def have_one(association_root)
|
19
|
+
HaveOneAssociationMatcher.new(association_root)
|
20
|
+
end
|
16
21
|
end
|
@@ -10,7 +10,7 @@ module ActiveModelSerializersMatchers
|
|
10
10
|
|
11
11
|
def matches?(actual)
|
12
12
|
@actual = actual.is_a?(Class) ? actual : actual.class
|
13
|
-
|
13
|
+
match_association? && match_key? && match_serializer?
|
14
14
|
end
|
15
15
|
|
16
16
|
def as(key)
|
@@ -33,18 +33,18 @@ module ActiveModelSerializersMatchers
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def failure_message
|
36
|
-
if
|
37
|
-
"expected #{actual} association options for #{root}: #{
|
36
|
+
if match_association?
|
37
|
+
"expected #{actual} association options for #{root}: #{root_association.options} to include: #{expected_association}"
|
38
38
|
else
|
39
|
-
"expected #{actual} associations: #{associations
|
39
|
+
"expected #{actual} associations: #{associations} to include: {#{root.inspect}=>(subclass of ActiveModel::Serializer::Associations::HasMany)}"
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
def failure_message_when_negated
|
44
|
-
if
|
45
|
-
"expected #{actual} associations: #{associations
|
44
|
+
if match_association?
|
45
|
+
"expected #{actual} associations: #{associations} to not include: {#{root.inspect}=>(subclass of ActiveModel::Serializer::Associations::HasMany)}"
|
46
46
|
else
|
47
|
-
"expected #{actual} association options for #{root}: #{
|
47
|
+
"expected #{actual} association options for #{root}: #{root_association.options} to not include: #{expected_options}"
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -61,23 +61,23 @@ module ActiveModelSerializersMatchers
|
|
61
61
|
actual._associations
|
62
62
|
end
|
63
63
|
|
64
|
-
def
|
65
|
-
return false if associations[root].nil?
|
64
|
+
def root_association
|
66
65
|
associations[root]
|
67
66
|
end
|
68
67
|
|
69
|
-
def
|
70
|
-
|
68
|
+
def match_association?
|
69
|
+
return false if root_association.nil?
|
70
|
+
root_association.superclass == ActiveModel::Serializer::Associations::HasMany
|
71
71
|
end
|
72
72
|
|
73
73
|
def match_key?
|
74
74
|
return true unless key_check
|
75
|
-
|
75
|
+
root_association.options[:key] == key
|
76
76
|
end
|
77
77
|
|
78
78
|
def match_serializer?
|
79
79
|
return true unless serializer_check
|
80
|
-
|
80
|
+
root_association.options[:serializer].to_s == serializer.to_s
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module ActiveModelSerializersMatchers
|
2
|
+
class HaveOneAssociationMatcher
|
3
|
+
attr_accessor :actual, :root, :key, :serializer, :key_check, :serializer_check
|
4
|
+
|
5
|
+
def initialize(root)
|
6
|
+
@root = root
|
7
|
+
@key_check = false
|
8
|
+
@serializer_check = false
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(actual)
|
12
|
+
@actual = actual.is_a?(Class) ? actual : actual.class
|
13
|
+
match_association? && match_key? && match_serializer?
|
14
|
+
end
|
15
|
+
|
16
|
+
def as(key)
|
17
|
+
self.key_check = true
|
18
|
+
self.key = key
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def serialized_with(serializer)
|
23
|
+
self.serializer_check = true
|
24
|
+
self.serializer = serializer
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def description
|
29
|
+
msg = "have one #{root}"
|
30
|
+
msg << " as \"#{key}\"" if key_check
|
31
|
+
msg << " serialized with #{serializer}" if serializer_check
|
32
|
+
msg
|
33
|
+
end
|
34
|
+
|
35
|
+
def failure_message
|
36
|
+
if match_association?
|
37
|
+
"expected #{actual} association options for #{root}: #{root_association.options} to include: #{expected_association}"
|
38
|
+
else
|
39
|
+
"expected #{actual} associations: #{associations} to include: {#{root.inspect}=>(subclass of ActiveModel::Serializer::Associations::HasOne)}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def failure_message_when_negated
|
44
|
+
if match_association?
|
45
|
+
"expected #{actual} associations: #{associations} to not include: {#{root.inspect}=>(subclass of ActiveModel::Serializer::Associations::HasOne)}"
|
46
|
+
else
|
47
|
+
"expected #{actual} association options for #{root}: #{root_association.options} to not include: #{expected_options}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def expected_options
|
52
|
+
expected = { root: root }
|
53
|
+
expected[:key] = key if key_check
|
54
|
+
expected[:serializer] = serializer if serializer_check
|
55
|
+
expected
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def associations
|
61
|
+
actual._associations
|
62
|
+
end
|
63
|
+
|
64
|
+
def root_association
|
65
|
+
associations[root]
|
66
|
+
end
|
67
|
+
|
68
|
+
def match_association?
|
69
|
+
return false if root_association.nil?
|
70
|
+
root_association.superclass == ActiveModel::Serializer::Associations::HasOne
|
71
|
+
end
|
72
|
+
|
73
|
+
def match_key?
|
74
|
+
return true unless key_check
|
75
|
+
root_association.options[:key] == key
|
76
|
+
end
|
77
|
+
|
78
|
+
def match_serializer?
|
79
|
+
return true unless serializer_check
|
80
|
+
root_association.options[:serializer].to_s == serializer.to_s
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -9,7 +9,7 @@ describe ActiveModelSerializersMatchers::HaveManyAssociationMatcher do
|
|
9
9
|
describe '#matches?' do
|
10
10
|
context 'when called with a class' do
|
11
11
|
it 'sets @actual to that class' do
|
12
|
-
allow(subject).to receive(:
|
12
|
+
allow(subject).to receive(:root_association)
|
13
13
|
subject.matches?(Object)
|
14
14
|
expect(subject.actual).to be Object
|
15
15
|
end
|
@@ -17,25 +17,24 @@ describe ActiveModelSerializersMatchers::HaveManyAssociationMatcher do
|
|
17
17
|
|
18
18
|
context 'when called with an instance of class' do
|
19
19
|
it 'sets @actual to the class of that instance' do
|
20
|
-
allow(subject).to receive(:
|
20
|
+
allow(subject).to receive(:root_association)
|
21
21
|
subject.matches?(Object.new)
|
22
22
|
expect(subject.actual).to be Object
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
describe 'return value' do
|
27
|
-
context 'when
|
27
|
+
context 'when match_association?, match_key?, and match_serializer? are all true' do
|
28
28
|
before do
|
29
|
-
expect(subject).to receive(:
|
30
|
-
expect(subject).to receive(:
|
31
|
-
expect(subject).to receive(:
|
32
|
-
expect(subject).to receive(:match_serializer?) { true }
|
29
|
+
expect(subject).to receive(:match_association?) { true }
|
30
|
+
expect(subject).to receive(:match_key? ) { true }
|
31
|
+
expect(subject).to receive(:match_serializer? ) { true }
|
33
32
|
end
|
34
33
|
specify { expect(subject.matches?(serializer)).to be true }
|
35
34
|
end
|
36
35
|
|
37
|
-
context 'when any
|
38
|
-
match_check_methods = [:
|
36
|
+
context 'when any match_association?, match_key?, or match_serializer? is false' do
|
37
|
+
match_check_methods = [:match_association?, :match_key?, :match_serializer?]
|
39
38
|
|
40
39
|
match_check_methods.each do |false_method|
|
41
40
|
before do
|
@@ -83,9 +82,17 @@ describe ActiveModelSerializersMatchers::HaveManyAssociationMatcher do
|
|
83
82
|
end
|
84
83
|
end
|
85
84
|
|
86
|
-
describe '
|
85
|
+
describe 'feature tests' do
|
87
86
|
include ActiveModelSerializersMatchers
|
88
87
|
|
88
|
+
context 'a serializer with one foo' do
|
89
|
+
subject do
|
90
|
+
Class.new(ActiveModel::Serializer) { has_one :foo }
|
91
|
+
end
|
92
|
+
it { should_not have_many :foo }
|
93
|
+
it { should_not have_many :foos }
|
94
|
+
end
|
95
|
+
|
89
96
|
context 'a serializer with many foos' do
|
90
97
|
subject do
|
91
98
|
Class.new(ActiveModel::Serializer) { has_many :foos }
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveModelSerializersMatchers::HaveOneAssociationMatcher do
|
4
|
+
|
5
|
+
subject { described_class.new(:foos) }
|
6
|
+
|
7
|
+
let(:serializer) { Class.new(ActiveModel::Serializer) }
|
8
|
+
|
9
|
+
describe '#matches?' do
|
10
|
+
context 'when called with a class' do
|
11
|
+
it 'sets @actual to that class' do
|
12
|
+
allow(subject).to receive(:root_association)
|
13
|
+
subject.matches?(Object)
|
14
|
+
expect(subject.actual).to be Object
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when called with an instance of class' do
|
19
|
+
it 'sets @actual to the class of that instance' do
|
20
|
+
allow(subject).to receive(:root_association)
|
21
|
+
subject.matches?(Object.new)
|
22
|
+
expect(subject.actual).to be Object
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'return value' do
|
27
|
+
context 'when match_association?, match_key?, and match_serializer? are all true' do
|
28
|
+
before do
|
29
|
+
expect(subject).to receive(:match_association?) { true }
|
30
|
+
expect(subject).to receive(:match_key? ) { true }
|
31
|
+
expect(subject).to receive(:match_serializer? ) { true }
|
32
|
+
end
|
33
|
+
specify { expect(subject.matches?(serializer)).to be true }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when any match_association?, match_key?, or match_serializer? is false' do
|
37
|
+
match_check_methods = [:match_association?, :match_key?, :match_serializer?]
|
38
|
+
|
39
|
+
match_check_methods.each do |false_method|
|
40
|
+
before do
|
41
|
+
match_check_methods.each do |method|
|
42
|
+
allow(subject).to receive(method) { method == false_method ? false : true }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
specify { expect(subject.matches?(serializer)).to be false }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#as' do
|
52
|
+
it 'should return self' do
|
53
|
+
expect(subject.as(:a_key)).to be subject
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'when it is not called' do
|
57
|
+
its(:key_check) { should be false }
|
58
|
+
its(:key) { should be_nil }
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when it is called with :a_key' do
|
62
|
+
before { subject.as(:a_key) }
|
63
|
+
its(:key_check) { should be true }
|
64
|
+
its(:key) { should be :a_key }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#serialized_with' do
|
69
|
+
it 'should return self' do
|
70
|
+
expect(subject.serialized_with(:a_serializer)).to be subject
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when it is not called' do
|
74
|
+
its(:serializer_check) { should be false }
|
75
|
+
its(:serializer) { should be_nil }
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when it is called with :a_serializer' do
|
79
|
+
before { subject.serialized_with(:a_serializer) }
|
80
|
+
its(:serializer_check) { should be true }
|
81
|
+
its(:serializer) { should be :a_serializer }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'feature tests' do
|
86
|
+
include ActiveModelSerializersMatchers
|
87
|
+
|
88
|
+
context 'a serializer with one foo' do
|
89
|
+
subject do
|
90
|
+
Class.new(ActiveModel::Serializer) { has_many :foos }
|
91
|
+
end
|
92
|
+
it { should_not have_one :foo }
|
93
|
+
it { should_not have_one :foos }
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'a serializer with one foo' do
|
97
|
+
subject do
|
98
|
+
Class.new(ActiveModel::Serializer) { has_one :foo }
|
99
|
+
end
|
100
|
+
it { should have_one(:foo) }
|
101
|
+
it { should_not have_one(:bar) }
|
102
|
+
it { should_not have_one(:foo).as(:bar) }
|
103
|
+
it { should_not have_one(:foo).serialized_with(:baz) }
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'a serializer with one foo as bar' do
|
107
|
+
subject do
|
108
|
+
Class.new(ActiveModel::Serializer) { has_one :foo, key: :bar }
|
109
|
+
end
|
110
|
+
it { should have_one(:foo) }
|
111
|
+
it { should have_one(:foo).as(:bar) }
|
112
|
+
it { should_not have_one(:foo).as(:baz) }
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'a serializer with one foo serialized with baz' do
|
116
|
+
subject do
|
117
|
+
Class.new(ActiveModel::Serializer) { has_one :foo, serializer: :baz }
|
118
|
+
end
|
119
|
+
it { should have_one(:foo) }
|
120
|
+
it { should have_one(:foo).serialized_with(:baz) }
|
121
|
+
it { should_not have_one(:foo).serialized_with(:bar) }
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'a serializer with one foo as bar serialized with baz' do
|
125
|
+
subject do
|
126
|
+
Class.new(ActiveModel::Serializer) { has_one :foo, key: :bar, serializer: :baz }
|
127
|
+
end
|
128
|
+
it { should have_one(:foo) }
|
129
|
+
it { should have_one(:foo).as(:bar) }
|
130
|
+
it { should have_one(:foo).serialized_with(:baz) }
|
131
|
+
it { should have_one(:foo).as(:bar).serialized_with(:baz) }
|
132
|
+
it { should_not have_one(:fuu).as(:bar).serialized_with(:baz) }
|
133
|
+
it { should_not have_one(:foo).as(:bor).serialized_with(:baz) }
|
134
|
+
it { should_not have_one(:foo).as(:bar).serialized_with(:biz) }
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -11,4 +11,12 @@ describe ActiveModelSerializersMatchers do
|
|
11
11
|
have_many(:association_root)
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
describe '#have_one' do
|
16
|
+
it 'instantiates a new HaveOneAssociationMatcher' do
|
17
|
+
expect(described_class::HaveOneAssociationMatcher)
|
18
|
+
.to receive(:new).with(:association_root)
|
19
|
+
have_one(:association_root)
|
20
|
+
end
|
21
|
+
end
|
14
22
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_serializers_matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Ta
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|
@@ -110,8 +110,10 @@ files:
|
|
110
110
|
- active_model_serializers_matchers.gemspec
|
111
111
|
- lib/active_model_serializers_matchers.rb
|
112
112
|
- lib/active_model_serializers_matchers/have_many_association_matcher.rb
|
113
|
+
- lib/active_model_serializers_matchers/have_one_association_matcher.rb
|
113
114
|
- lib/active_model_serializers_matchers/version.rb
|
114
115
|
- spec/active_model_serializers_matchers/have_many_association_matcher_spec.rb
|
116
|
+
- spec/active_model_serializers_matchers/have_one_association_matcher_spec.rb
|
115
117
|
- spec/active_model_serializers_matchers_spec.rb
|
116
118
|
- spec/spec_helper.rb
|
117
119
|
homepage: ''
|
@@ -140,5 +142,6 @@ specification_version: 4
|
|
140
142
|
summary: RSpec matchers for testing ActiveModel::Serializer
|
141
143
|
test_files:
|
142
144
|
- spec/active_model_serializers_matchers/have_many_association_matcher_spec.rb
|
145
|
+
- spec/active_model_serializers_matchers/have_one_association_matcher_spec.rb
|
143
146
|
- spec/active_model_serializers_matchers_spec.rb
|
144
147
|
- spec/spec_helper.rb
|