active_model_serializers_matchers 0.0.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +9 -0
- data/README.md +82 -22
- data/Rakefile +6 -1
- data/active_model_serializers_matchers.gemspec +7 -6
- data/lib/active_model_serializers_matchers.rb +3 -11
- data/lib/active_model_serializers_matchers/association_matcher.rb +175 -0
- data/lib/active_model_serializers_matchers/version.rb +1 -1
- data/spec/active_model_serializers_matchers/association_matcher_spec.rb +201 -0
- data/spec/active_model_serializers_matchers_spec.rb +12 -10
- data/spec/spec_helper.rb +9 -2
- data/spec/support/dummy_serializers.rb +2 -0
- data/spec/support/rspec_fail_matchers.rb +20 -0
- data/spec/support/shared_examples.rb +127 -0
- metadata +19 -29
- data/lib/active_model_serializers_matchers/have_many_association_matcher.rb +0 -83
- data/lib/active_model_serializers_matchers/have_one_association_matcher.rb +0 -83
- data/spec/active_model_serializers_matchers/have_many_association_matcher_spec.rb +0 -137
- data/spec/active_model_serializers_matchers/have_one_association_matcher_spec.rb +0 -137
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35a767c68d2ab67433d5bf50aba99b4c19f3943d
|
4
|
+
data.tar.gz: d7279bebe1091d1d3ed3dccc0d2ef71e15a2bc6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20177840149749d3004b77bf4e7d88d010eb889c4e789202b723bad9515559eb9d1aa7afe31dc3abea2a3e95855d604425909d67023c0a1d5196264a2fc25db2
|
7
|
+
data.tar.gz: 279ccdf1a59374061e675fa282e568dfd7c307196167d476a61d485afe2f527e40c7b3169157f4a58422a31aa0706ecca876cfa927944b3e15c646d82eba4f28
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,13 +1,22 @@
|
|
1
1
|
# ActiveModelSerializersMatchers
|
2
|
+
[![Gem Version][gem_version_badge]][rubygems]
|
3
|
+
[![Travis CI][travis_badge]][travis]
|
4
|
+
[![Coverage Status][coverage_badge]][coverage]
|
5
|
+
[![Code Climate][codeclimate_badge]][codeclimate]
|
6
|
+
### RSpec Matchers for ActiveModel::Serializer Associations
|
2
7
|
|
3
|
-
|
8
|
+
**Note:** This gem requires `"active_model_serializers", "~> 0.8.0"`:
|
9
|
+
- [v0.8.0](https://github.com/rails-api/active_model_serializers/tree/v0.8.0)
|
10
|
+
- [v0.8.1](https://github.com/rails-api/active_model_serializers/tree/v0.8.1)
|
11
|
+
- [v0.8.2](https://github.com/rails-api/active_model_serializers/tree/v0.8.2)
|
12
|
+
- [v0.8.3](https://github.com/rails-api/active_model_serializers/tree/v0.8.3)
|
4
13
|
|
5
|
-
|
14
|
+
### Installation
|
6
15
|
|
7
16
|
Add this line to your application's Gemfile:
|
8
17
|
|
9
18
|
```ruby
|
10
|
-
gem 'active_model_serializers_matchers', '0.0
|
19
|
+
gem 'active_model_serializers_matchers', '0.2.0'
|
11
20
|
```
|
12
21
|
|
13
22
|
And then execute:
|
@@ -18,64 +27,103 @@ Or install it yourself as:
|
|
18
27
|
|
19
28
|
$ gem install active_model_serializers_matchers
|
20
29
|
|
30
|
+
### Configure RSpec
|
31
|
+
``` ruby
|
32
|
+
RSpec.configure do |config|
|
33
|
+
config.include ActiveModelSerializersMatchers, :type => :serializer
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
21
37
|
## Usage
|
22
|
-
|
38
|
+
|
39
|
+
### Associations
|
40
|
+
|
41
|
+
#### has_one and has_many associations
|
42
|
+
|
43
|
+
association matcher: `#have_one` and `#have_many`
|
44
|
+
|
23
45
|
``` ruby
|
24
46
|
class ListSerializer < ActiveModel::Serializer
|
25
47
|
has_one :title
|
26
48
|
has_many :items
|
27
49
|
end
|
28
50
|
|
29
|
-
RSpec.describe ListSerializer do
|
51
|
+
RSpec.describe ListSerializer, :type => :serializer do
|
52
|
+
subject { described_class }
|
30
53
|
it { should have_one(:title) }
|
31
54
|
it { should have_many(:items) }
|
55
|
+
it { should have_many(:cats) }
|
32
56
|
end
|
33
57
|
|
34
|
-
|
35
|
-
|
58
|
+
# ListSerializer
|
59
|
+
# should have one :title
|
60
|
+
# should have many :items
|
61
|
+
# should have many :cats (FAILED - 1)
|
36
62
|
```
|
37
63
|
|
38
|
-
|
39
|
-
|
40
|
-
|
64
|
+
#### key option
|
65
|
+
|
66
|
+
option matcher: `#as`
|
67
|
+
|
41
68
|
``` ruby
|
42
69
|
class ShoeRackSerializer < ActiveModel::Serializer
|
43
70
|
has_many :shoes, key: :kicks
|
44
71
|
end
|
45
72
|
|
46
|
-
RSpec.describe ShoeRackSerializer do
|
73
|
+
RSpec.describe ShoeRackSerializer, :type => :serializer do
|
74
|
+
subject { described_class }
|
47
75
|
it { should have_many(:shoes).as(:kicks) }
|
76
|
+
it { should have_many(:shoes).as(:ones_and_twos) }
|
48
77
|
end
|
49
78
|
|
50
|
-
|
79
|
+
# ShoeRackSerializer
|
80
|
+
# should have many :shoes as :kicks
|
81
|
+
# should have many :shoes as :ones_and_twos (FAILED - 1)
|
51
82
|
```
|
52
|
-
|
53
|
-
|
83
|
+
|
84
|
+
#### serializer option
|
85
|
+
|
86
|
+
option matcher: `#serialized_with`
|
87
|
+
|
54
88
|
``` ruby
|
89
|
+
class ProductSerializer < ActiveModel::Serializer; end
|
90
|
+
class SoupCanSerializer < ActiveModel::Serializer; end
|
91
|
+
|
55
92
|
class ShoppingCartSerializer < ActiveModel::Serializer
|
56
93
|
has_many :items, serializer: ProductSerializer
|
57
94
|
end
|
58
95
|
|
59
|
-
RSpec.describe ShoppingCartSerializer do
|
96
|
+
RSpec.describe ShoppingCartSerializer, :type => :serializer do
|
97
|
+
subject { described_class }
|
60
98
|
it { should have_many(:items).serialized_with(ProductSerializer) }
|
99
|
+
it { should have_many(:items).serialized_with(SoupCanSerializer) }
|
61
100
|
end
|
62
101
|
|
63
|
-
|
102
|
+
# ShoppingCartSerializer
|
103
|
+
# should have many :items serialized with ProductSerializer
|
104
|
+
# should have many :items serialized with SoupCanSerializer (FAILED - 1)
|
64
105
|
```
|
65
|
-
|
66
|
-
|
106
|
+
|
107
|
+
#### chaining multiple matchers
|
108
|
+
|
109
|
+
Multiple option matchers can be chained onto an association matcher in any order.
|
110
|
+
|
67
111
|
``` ruby
|
112
|
+
class FoodSerializer < ActiveModel::Serializer; end
|
113
|
+
|
68
114
|
class MenuSerializer < ActiveModel::Serializer
|
69
|
-
has_many :entrees, key: dishes, serializer: FoodSerializer
|
115
|
+
has_many :entrees, key: :dishes, serializer: FoodSerializer
|
70
116
|
end
|
71
117
|
|
72
|
-
RSpec.describe MenuSerializer do
|
118
|
+
RSpec.describe MenuSerializer, :type => :serializer do
|
119
|
+
subject { described_class }
|
73
120
|
it { should have_many(:entrees).as(:dishes).serialized_with(FoodSerializer) }
|
74
121
|
it { should have_many(:entrees).serialized_with(FoodSerializer).as(:dishes) }
|
75
122
|
end
|
76
123
|
|
77
|
-
|
78
|
-
|
124
|
+
# MenuSerializer
|
125
|
+
# should have many :entrees as :dishes serialized with FoodSerializer
|
126
|
+
# should have many :entrees serialized with FoodSerializer as :dishes
|
79
127
|
```
|
80
128
|
|
81
129
|
## Contributing
|
@@ -85,3 +133,15 @@ end
|
|
85
133
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
86
134
|
4. Push to the branch (`git push origin my-new-feature`)
|
87
135
|
5. Create a new Pull Request
|
136
|
+
|
137
|
+
[gem_version_badge]: http://img.shields.io/gem/v/active_model_serializers_matchers.svg?style=flat
|
138
|
+
[rubygems]: http://rubygems.org/gems/active_model_serializers_matchers
|
139
|
+
|
140
|
+
[travis_badge]: http://img.shields.io/travis/tonyta/active_model_serializers_matchers.svg?style=flat
|
141
|
+
[travis]: https://travis-ci.org/tonyta/active_model_serializers_matchers
|
142
|
+
|
143
|
+
[coverage_badge]: https://img.shields.io/coveralls/tonyta/active_model_serializers_matchers.svg?style=flat
|
144
|
+
[coverage]: https://coveralls.io/r/tonyta/active_model_serializers_matchers?branch=objectify-association-matchers
|
145
|
+
|
146
|
+
[codeclimate_badge]: https://img.shields.io/codeclimate/github/tonyta/active_model_serializers_matchers.svg?style=flat
|
147
|
+
[codeclimate]: https://codeclimate.com/github/tonyta/active_model_serializers_matchers
|
data/Rakefile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'active_model_serializers_matchers/version'
|
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = ActiveModelSerializersMatchers::VERSION
|
9
9
|
spec.authors = ["Tony Ta"]
|
10
10
|
spec.email = ["tonyta.tt@gmail.com"]
|
11
|
-
spec.
|
12
|
-
spec.
|
11
|
+
spec.description = "RSpec Matchers for ActiveModel::Serializer Associations"
|
12
|
+
spec.summary = "These matchers will allow you to test associations between serializers."
|
13
13
|
spec.homepage = ""
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -18,11 +18,12 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.
|
21
|
+
spec.required_ruby_version = '>= 1.9.3'
|
22
|
+
|
23
|
+
spec.add_dependency "active_model_serializers", "0.8.0"
|
22
24
|
spec.add_dependency "rspec", "~> 3.0"
|
23
25
|
|
24
26
|
spec.add_development_dependency "bundler", "~> 1.7"
|
25
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
-
spec.add_development_dependency "
|
27
|
-
spec.add_development_dependency "pry-byebug"
|
28
|
+
spec.add_development_dependency "coveralls"
|
28
29
|
end
|
@@ -1,21 +1,13 @@
|
|
1
|
-
require "active_support"
|
2
|
-
|
3
1
|
require "active_model_serializers_matchers/version"
|
4
|
-
require "active_model_serializers_matchers/
|
5
|
-
require "active_model_serializers_matchers/have_one_association_matcher"
|
2
|
+
require "active_model_serializers_matchers/association_matcher"
|
6
3
|
|
7
4
|
module ActiveModelSerializersMatchers
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
|
10
|
-
included do
|
11
|
-
subject { described_class }
|
12
|
-
end
|
13
5
|
|
14
6
|
def have_many(association_root)
|
15
|
-
|
7
|
+
AssociationMatcher.new(association_root, :has_many)
|
16
8
|
end
|
17
9
|
|
18
10
|
def have_one(association_root)
|
19
|
-
|
11
|
+
AssociationMatcher.new(association_root, :has_one)
|
20
12
|
end
|
21
13
|
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
module ActiveModelSerializersMatchers
|
2
|
+
class AssociationMatcher
|
3
|
+
|
4
|
+
attr_reader :root, :type, :checks
|
5
|
+
attr_reader :actual
|
6
|
+
|
7
|
+
def initialize(root, type)
|
8
|
+
@root = root
|
9
|
+
@type = type
|
10
|
+
@checks = [AssociationCheck.new(self, type)]
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(actual)
|
14
|
+
@actual = actual.is_a?(Class) ? actual : actual.class
|
15
|
+
checks.all?(&:pass?)
|
16
|
+
end
|
17
|
+
|
18
|
+
def description
|
19
|
+
checks.map(&:description).join(' ')
|
20
|
+
end
|
21
|
+
|
22
|
+
def failure_message
|
23
|
+
checks.find(&:fail?).failure_message
|
24
|
+
end
|
25
|
+
|
26
|
+
def root_association
|
27
|
+
associations[root]
|
28
|
+
end
|
29
|
+
|
30
|
+
def as(key)
|
31
|
+
checks << KeyCheck.new(self, key)
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
def serialized_with(serializer)
|
36
|
+
checks << SerializerCheck.new(self, serializer)
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def associations
|
43
|
+
actual._associations
|
44
|
+
end
|
45
|
+
|
46
|
+
class AssociationCheck
|
47
|
+
|
48
|
+
attr_reader :matcher, :type
|
49
|
+
|
50
|
+
def initialize(matcher, type)
|
51
|
+
@matcher = matcher
|
52
|
+
@type = type
|
53
|
+
end
|
54
|
+
|
55
|
+
def pass?
|
56
|
+
return false if matcher.root_association.nil?
|
57
|
+
matcher.root_association.superclass == association_type
|
58
|
+
end
|
59
|
+
|
60
|
+
def fail?
|
61
|
+
!pass?
|
62
|
+
end
|
63
|
+
|
64
|
+
def description
|
65
|
+
"#{ association_string } #{ matcher.root.inspect }"
|
66
|
+
end
|
67
|
+
|
68
|
+
def failure_message
|
69
|
+
"expected #{ matcher.actual } to define a '#{ type } #{ matcher.root.inspect }' association"
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def association_type
|
75
|
+
case type
|
76
|
+
when :has_one
|
77
|
+
ActiveModel::Serializer::Associations::HasOne
|
78
|
+
when :has_many
|
79
|
+
ActiveModel::Serializer::Associations::HasMany
|
80
|
+
else
|
81
|
+
raise ArgumentError, "'#{type}' is an invalid association type."
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def association_string
|
86
|
+
case type
|
87
|
+
when :has_one
|
88
|
+
'have one'
|
89
|
+
when :has_many
|
90
|
+
'have many'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class KeyCheck
|
96
|
+
|
97
|
+
attr_reader :matcher, :key
|
98
|
+
|
99
|
+
def initialize(matcher, key)
|
100
|
+
@matcher = matcher
|
101
|
+
@key = key
|
102
|
+
end
|
103
|
+
|
104
|
+
def pass?
|
105
|
+
actual_key.to_s == key.to_s
|
106
|
+
end
|
107
|
+
|
108
|
+
def fail?
|
109
|
+
!pass?
|
110
|
+
end
|
111
|
+
|
112
|
+
def description
|
113
|
+
"as #{ key.inspect }"
|
114
|
+
end
|
115
|
+
|
116
|
+
def failure_message
|
117
|
+
"expected #{ matcher.actual } '#{ matcher.type } #{ matcher.root.inspect }' association to explicitly have key #{ key.inspect } but instead #{ actual_key_string }"
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
|
122
|
+
def actual_key
|
123
|
+
matcher.root_association.options[:key]
|
124
|
+
end
|
125
|
+
|
126
|
+
def actual_key_string
|
127
|
+
if actual_key
|
128
|
+
"was #{ actual_key.inspect }"
|
129
|
+
else
|
130
|
+
'has none'
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
class SerializerCheck
|
136
|
+
|
137
|
+
attr_reader :matcher, :serializer
|
138
|
+
|
139
|
+
def initialize(matcher, serializer)
|
140
|
+
@matcher = matcher
|
141
|
+
@serializer = serializer
|
142
|
+
end
|
143
|
+
|
144
|
+
def pass?
|
145
|
+
actual_serializer.to_s == serializer.to_s
|
146
|
+
end
|
147
|
+
|
148
|
+
def fail?
|
149
|
+
!pass?
|
150
|
+
end
|
151
|
+
|
152
|
+
def description
|
153
|
+
"serialized with #{ serializer }"
|
154
|
+
end
|
155
|
+
|
156
|
+
def failure_message
|
157
|
+
"expected #{ matcher.actual } '#{ matcher.type } #{ matcher.root.inspect }' association to explicitly have serializer #{ serializer } but instead #{ actual_serializer_string }"
|
158
|
+
end
|
159
|
+
|
160
|
+
private
|
161
|
+
|
162
|
+
def actual_serializer
|
163
|
+
matcher.root_association.options[:serializer]
|
164
|
+
end
|
165
|
+
|
166
|
+
def actual_serializer_string
|
167
|
+
if actual_serializer
|
168
|
+
"was #{ actual_serializer }"
|
169
|
+
else
|
170
|
+
"has none"
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveModelSerializersMatchers::AssociationMatcher do
|
4
|
+
|
5
|
+
context 'when given serializer that has_one :foo' do
|
6
|
+
subject do
|
7
|
+
Class.new(ActiveModel::Serializer) { has_one :foo }
|
8
|
+
end
|
9
|
+
it_behaves_like 'it has_one :foo'
|
10
|
+
it_behaves_like 'it has_one :foo and no key'
|
11
|
+
it_behaves_like 'it has_one :foo and no serializer'
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when given serializer that has_one :foo, key: :bar' do
|
15
|
+
subject do
|
16
|
+
Class.new(ActiveModel::Serializer) { has_one :foo, key: :bar }
|
17
|
+
end
|
18
|
+
it_behaves_like 'it has_one :foo'
|
19
|
+
it_behaves_like 'it has_one :foo and no serializer'
|
20
|
+
it_behaves_like 'it has_one :foo with key :bar'
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'when given serializer that has_one :foo, serializer: FooSerializer' do
|
24
|
+
subject do
|
25
|
+
Class.new(ActiveModel::Serializer) { has_one :foo, serializer: FooSerializer }
|
26
|
+
end
|
27
|
+
it_behaves_like 'it has_one :foo'
|
28
|
+
it_behaves_like 'it has_one :foo and no key'
|
29
|
+
it_behaves_like 'it has_one :foo with serializer FooSerializer'
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when given serializer that has_one :foo, key: :bar, serializer: FooSerializer' do
|
33
|
+
subject do
|
34
|
+
Class.new(ActiveModel::Serializer) { has_one :foo, key: :bar, serializer: FooSerializer }
|
35
|
+
end
|
36
|
+
it_behaves_like 'it has_one :foo'
|
37
|
+
it_behaves_like 'it has_one :foo with key :bar'
|
38
|
+
it_behaves_like 'it has_one :foo with serializer FooSerializer'
|
39
|
+
|
40
|
+
describe 'should if all chained expectations pass in any order' do
|
41
|
+
it 'should pass a have_one :foo as :bar serialized_with FooSerializer expectation' do
|
42
|
+
expectation = have_one(:foo).as(:bar).serialized_with(FooSerializer)
|
43
|
+
expect(subject).to expectation
|
44
|
+
expect(expectation.description).to eq('have one :foo as :bar serialized with FooSerializer')
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should pass a have_one :foo serialized_with FooSerializer as :bar expectation' do
|
48
|
+
expectation = have_one(:foo).serialized_with(FooSerializer).as(:bar)
|
49
|
+
expect(subject).to expectation
|
50
|
+
expect(expectation.description).to eq('have one :foo serialized with FooSerializer as :bar')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'should fail with the first encountered expectation message if any chained expectations fail' do
|
55
|
+
it 'should fail a have_one :fuu expectation' do
|
56
|
+
failure_message = "expected #{ subject } to define a 'has_one :fuu' association"
|
57
|
+
expect {
|
58
|
+
expect(subject).to have_one(:fuu).as(:bar).serialized_with(FooSerializer)
|
59
|
+
}.to fail_with(failure_message)
|
60
|
+
expect {
|
61
|
+
expect(subject).to have_one(:fuu).serialized_with(FooSerializer).as(:bar)
|
62
|
+
}.to fail_with(failure_message)
|
63
|
+
expect {
|
64
|
+
expect(subject).to have_one(:fuu).as(:bor).serialized_with(BarSerializer)
|
65
|
+
}.to fail_with(failure_message)
|
66
|
+
expect {
|
67
|
+
expect(subject).to have_one(:fuu).serialized_with(BarSerializer).as(:bor)
|
68
|
+
}.to fail_with(failure_message)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should fail a have_one :foo serialized with BarSerializer expectation' do
|
72
|
+
failure_message = "expected #{ subject } 'has_one :foo' association to explicitly have serializer BarSerializer but instead was FooSerializer"
|
73
|
+
expect {
|
74
|
+
expect(subject).to have_one(:foo).as(:bar).serialized_with(BarSerializer)
|
75
|
+
}.to fail_with(failure_message)
|
76
|
+
expect {
|
77
|
+
expect(subject).to have_one(:foo).serialized_with(BarSerializer).as(:bar)
|
78
|
+
}.to fail_with(failure_message)
|
79
|
+
expect {
|
80
|
+
expect(subject).to have_one(:foo).serialized_with(BarSerializer).as(:bor)
|
81
|
+
}.to fail_with(failure_message)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should fail a have_one :foo as :bor expectation' do
|
85
|
+
failure_message = "expected #{ subject } 'has_one :foo' association to explicitly have key :bor but instead was :bar"
|
86
|
+
expect {
|
87
|
+
expect(subject).to have_one(:foo).serialized_with(FooSerializer).as(:bor)
|
88
|
+
}.to fail_with(failure_message)
|
89
|
+
expect {
|
90
|
+
expect(subject).to have_one(:foo).as(:bor).serialized_with(FooSerializer)
|
91
|
+
}.to fail_with(failure_message)
|
92
|
+
expect {
|
93
|
+
expect(subject).to have_one(:foo).as(:bor).serialized_with(BarSerializer)
|
94
|
+
}.to fail_with(failure_message)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'when given serializer that has_many :foos' do
|
100
|
+
subject do
|
101
|
+
Class.new(ActiveModel::Serializer) { has_many :foos }
|
102
|
+
end
|
103
|
+
it_behaves_like 'it has_many :foos'
|
104
|
+
it_behaves_like 'it has_many :foos and no key'
|
105
|
+
it_behaves_like 'it has_many :foos and no serializer'
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'when given serializer that has_many :foos, key: :bars' do
|
109
|
+
subject do
|
110
|
+
Class.new(ActiveModel::Serializer) { has_many :foos, key: :bars }
|
111
|
+
end
|
112
|
+
it_behaves_like 'it has_many :foos'
|
113
|
+
it_behaves_like 'it has_many :foos and no serializer'
|
114
|
+
it_behaves_like 'it has_many :foos with key :bars'
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'when given serializer that has_many :foos, serializer: FooSerializer' do
|
118
|
+
subject do
|
119
|
+
Class.new(ActiveModel::Serializer) { has_many :foos, serializer: FooSerializer }
|
120
|
+
end
|
121
|
+
it_behaves_like 'it has_many :foos'
|
122
|
+
it_behaves_like 'it has_many :foos and no key'
|
123
|
+
it_behaves_like 'it has_many :foos with serializer FooSerializer'
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'when given serializer that has_many :foos, key: :bars, serializer: FooSerializer' do
|
127
|
+
subject do
|
128
|
+
Class.new(ActiveModel::Serializer) { has_many :foos, key: :bars, serializer: FooSerializer }
|
129
|
+
end
|
130
|
+
it_behaves_like 'it has_many :foos'
|
131
|
+
it_behaves_like 'it has_many :foos with key :bars'
|
132
|
+
it_behaves_like 'it has_many :foos with serializer FooSerializer'
|
133
|
+
end
|
134
|
+
|
135
|
+
context 'when given serializer that has_many :foos, key: :bars, serializer: FooSerializer' do
|
136
|
+
subject do
|
137
|
+
Class.new(ActiveModel::Serializer) { has_many :foos, key: :bars, serializer: FooSerializer }
|
138
|
+
end
|
139
|
+
it_behaves_like 'it has_many :foos'
|
140
|
+
it_behaves_like 'it has_many :foos with key :bars'
|
141
|
+
it_behaves_like 'it has_many :foos with serializer FooSerializer'
|
142
|
+
|
143
|
+
describe 'should if all chained expectations pass in any order' do
|
144
|
+
it 'should pass a have_many :foos as :bars serialized_with FooSerializer expectation' do
|
145
|
+
expectation = have_many(:foos).as(:bars).serialized_with(FooSerializer)
|
146
|
+
expect(subject).to expectation
|
147
|
+
expect(expectation.description).to eq('have many :foos as :bars serialized with FooSerializer')
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'should pass a have_many :foos serialized_with FooSerializer as :bars expectation' do
|
151
|
+
expectation = have_many(:foos).serialized_with(FooSerializer).as(:bars)
|
152
|
+
expect(subject).to expectation
|
153
|
+
expect(expectation.description).to eq('have many :foos serialized with FooSerializer as :bars')
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe 'should fail with the first encountered expectation message if any chained expectations fail' do
|
158
|
+
it 'should fail a have_many :fuu expectation' do
|
159
|
+
failure_message = "expected #{ subject } to define a 'has_many :fuus' association"
|
160
|
+
expect {
|
161
|
+
expect(subject).to have_many(:fuus).as(:bars).serialized_with(FooSerializer)
|
162
|
+
}.to fail_with(failure_message)
|
163
|
+
expect {
|
164
|
+
expect(subject).to have_many(:fuus).serialized_with(FooSerializer).as(:bars)
|
165
|
+
}.to fail_with(failure_message)
|
166
|
+
expect {
|
167
|
+
expect(subject).to have_many(:fuus).as(:bors).serialized_with(BarSerializer)
|
168
|
+
}.to fail_with(failure_message)
|
169
|
+
expect {
|
170
|
+
expect(subject).to have_many(:fuus).serialized_with(BarSerializer).as(:bors)
|
171
|
+
}.to fail_with(failure_message)
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'should fail a have_many :foos serialized with BarSerializer expectation' do
|
175
|
+
failure_message = "expected #{ subject } 'has_many :foos' association to explicitly have serializer BarSerializer but instead was FooSerializer"
|
176
|
+
expect {
|
177
|
+
expect(subject).to have_many(:foos).as(:bars).serialized_with(BarSerializer)
|
178
|
+
}.to fail_with(failure_message)
|
179
|
+
expect {
|
180
|
+
expect(subject).to have_many(:foos).serialized_with(BarSerializer).as(:bars)
|
181
|
+
}.to fail_with(failure_message)
|
182
|
+
expect {
|
183
|
+
expect(subject).to have_many(:foos).serialized_with(BarSerializer).as(:bors)
|
184
|
+
}.to fail_with(failure_message)
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'should fail a have_many :foos as :bors expectation' do
|
188
|
+
failure_message = "expected #{ subject } 'has_many :foos' association to explicitly have key :bors but instead was :bars"
|
189
|
+
expect {
|
190
|
+
expect(subject).to have_many(:foos).serialized_with(FooSerializer).as(:bors)
|
191
|
+
}.to fail_with(failure_message)
|
192
|
+
expect {
|
193
|
+
expect(subject).to have_many(:foos).as(:bors).serialized_with(FooSerializer)
|
194
|
+
}.to fail_with(failure_message)
|
195
|
+
expect {
|
196
|
+
expect(subject).to have_many(:foos).as(:bors).serialized_with(BarSerializer)
|
197
|
+
}.to fail_with(failure_message)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|