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
@@ -2,21 +2,23 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe ActiveModelSerializersMatchers do
|
4
4
|
|
5
|
-
include described_class
|
6
|
-
|
7
5
|
describe '#have_many' do
|
8
|
-
it '
|
9
|
-
expect(described_class::
|
10
|
-
.to receive(:new).with(:association_root)
|
11
|
-
|
6
|
+
it 'returns a new AssociationMatcher of type :has_many' do
|
7
|
+
expect(described_class::AssociationMatcher)
|
8
|
+
.to receive(:new).with(:association_root, :has_many)
|
9
|
+
.and_call_original
|
10
|
+
expect(have_many(:association_root))
|
11
|
+
.to be_a described_class::AssociationMatcher
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
describe '#have_one' do
|
16
|
-
it '
|
17
|
-
expect(described_class::
|
18
|
-
.to receive(:new).with(:association_root)
|
19
|
-
|
16
|
+
it 'returns a new AssociationMatcher of type :has_one' do
|
17
|
+
expect(described_class::AssociationMatcher)
|
18
|
+
.to receive(:new).with(:association_root, :has_one)
|
19
|
+
.and_call_original
|
20
|
+
expect(have_one(:association_root))
|
21
|
+
.to be_a described_class::AssociationMatcher
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,11 @@
|
|
1
|
-
require '
|
2
|
-
|
1
|
+
require 'coveralls'
|
2
|
+
Coveralls.wear!
|
3
|
+
|
3
4
|
require 'active_model_serializers'
|
4
5
|
require 'active_model_serializers_matchers'
|
6
|
+
|
7
|
+
Dir[Pathname(__FILE__).join('../support/**/*.rb')].each { |f| require f }
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.include ActiveModelSerializersMatchers
|
11
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
def fail
|
4
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError)
|
5
|
+
end
|
6
|
+
|
7
|
+
def fail_with(message)
|
8
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError, message)
|
9
|
+
end
|
10
|
+
|
11
|
+
def fail_matching(message)
|
12
|
+
if String === message
|
13
|
+
regexp = /#{Regexp.escape(message)}/
|
14
|
+
else
|
15
|
+
regexp = message
|
16
|
+
end
|
17
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError, regexp)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
shared_examples 'it has_one :foo' do
|
2
|
+
it 'should pass a have_one :foo expectation' do
|
3
|
+
expectation = have_one(:foo)
|
4
|
+
expect(subject).to expectation
|
5
|
+
expect(expectation.description).to eq('have one :foo')
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should fail a have_one :fuu expectation' do
|
9
|
+
expect {
|
10
|
+
expect(subject).to have_one(:fuu)
|
11
|
+
}.to fail_with("expected #{ subject } to define a 'has_one :fuu' association")
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should fail a have_many :foo expectation' do
|
15
|
+
expect {
|
16
|
+
expect(subject).to have_many(:foo)
|
17
|
+
}.to fail_with("expected #{ subject } to define a 'has_many :foo' association")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
shared_examples 'it has_one :foo and no key' do
|
22
|
+
it 'should fail a have_one :foo as :bar expectation' do
|
23
|
+
expect {
|
24
|
+
expect(subject).to have_one(:foo).as(:bar)
|
25
|
+
}.to fail_with("expected #{ subject } 'has_one :foo' association to explicitly have key :bar but instead has none")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
shared_examples 'it has_one :foo and no serializer' do
|
30
|
+
it 'should fail a have_one :foo serialized with FooSerializer expectation' do
|
31
|
+
expect {
|
32
|
+
expect(subject).to have_one(:foo).serialized_with(FooSerializer)
|
33
|
+
}.to fail_with("expected #{ subject } 'has_one :foo' association to explicitly have serializer FooSerializer but instead has none")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
shared_examples 'it has_one :foo with key :bar' do
|
38
|
+
it 'should pass a have_one :foo as :bar expectation' do
|
39
|
+
expectation = have_one(:foo).as(:bar)
|
40
|
+
expect(subject).to expectation
|
41
|
+
expect(expectation.description).to eq('have one :foo as :bar')
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should fail a have_one :foo as :bor expectation' do
|
45
|
+
expect {
|
46
|
+
expect(subject).to have_one(:foo).as(:bor)
|
47
|
+
}.to fail_with("expected #{ subject } 'has_one :foo' association to explicitly have key :bor but instead was :bar")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
shared_examples 'it has_one :foo with serializer FooSerializer' do
|
52
|
+
it 'should pass a have_one :foo as :bar expectation' do
|
53
|
+
expectation = have_one(:foo).serialized_with(FooSerializer)
|
54
|
+
expect(subject).to expectation
|
55
|
+
expect(expectation.description).to eq('have one :foo serialized with FooSerializer')
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should fail a have_one :foo serialized with FooSerializer expectation' do
|
59
|
+
expect {
|
60
|
+
expect(subject).to have_one(:foo).serialized_with(BarSerializer)
|
61
|
+
}.to fail_with("expected #{ subject } 'has_one :foo' association to explicitly have serializer BarSerializer but instead was FooSerializer")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
shared_examples 'it has_many :foos' do
|
66
|
+
it 'should pass a have_many :foos expectation' do
|
67
|
+
expectation = have_many(:foos)
|
68
|
+
expect(subject).to expectation
|
69
|
+
expect(expectation.description).to eq('have many :foos')
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should fail a have_many :fuus expectation' do
|
73
|
+
expect {
|
74
|
+
expect(subject).to have_many(:fuus)
|
75
|
+
}.to fail_with("expected #{ subject } to define a 'has_many :fuus' association")
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should fail a have_one :foos expectation' do
|
79
|
+
expect {
|
80
|
+
expect(subject).to have_one(:foos)
|
81
|
+
}.to fail_with("expected #{ subject } to define a 'has_one :foos' association")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
shared_examples 'it has_many :foos and no key' do
|
86
|
+
it 'should fail a have_many :foos as :bars expectation' do
|
87
|
+
expect {
|
88
|
+
expect(subject).to have_many(:foos).as(:bars)
|
89
|
+
}.to fail_with("expected #{ subject } 'has_many :foos' association to explicitly have key :bars but instead has none")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
shared_examples 'it has_many :foos and no serializer' do
|
94
|
+
it 'should fail a have_many :foos serialized with FooSerializer expectation' do
|
95
|
+
expect {
|
96
|
+
expect(subject).to have_many(:foos).serialized_with(FooSerializer)
|
97
|
+
}.to fail_with("expected #{ subject } 'has_many :foos' association to explicitly have serializer FooSerializer but instead has none")
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
shared_examples 'it has_many :foos with key :bars' do
|
102
|
+
it 'should pass a have_many :foos as :bars expectation' do
|
103
|
+
expectation = have_many(:foos).as(:bars)
|
104
|
+
expect(subject).to expectation
|
105
|
+
expect(expectation.description).to eq('have many :foos as :bars')
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'should fail a have_many :foos as :bors expectation' do
|
109
|
+
expect {
|
110
|
+
expect(subject).to have_many(:foos).as(:bors)
|
111
|
+
}.to fail_with("expected #{ subject } 'has_many :foos' association to explicitly have key :bors but instead was :bars")
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
shared_examples 'it has_many :foos with serializer FooSerializer' do
|
116
|
+
it 'should pass a have_many :foos as :bar expectation' do
|
117
|
+
expectation = have_many(:foos).serialized_with(FooSerializer)
|
118
|
+
expect(subject).to expectation
|
119
|
+
expect(expectation.description).to eq('have many :foos serialized with FooSerializer')
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'should fail a have_many :foos serialized with FooSerializer expectation' do
|
123
|
+
expect {
|
124
|
+
expect(subject).to have_many(:foos).serialized_with(BarSerializer)
|
125
|
+
}.to fail_with("expected #{ subject } 'has_many :foos' association to explicitly have serializer BarSerializer but instead was FooSerializer")
|
126
|
+
end
|
127
|
+
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
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.2.0
|
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-
|
11
|
+
date: 2014-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 0.8.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.8.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -67,21 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '10.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: pry-byebug
|
70
|
+
name: coveralls
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
86
72
|
requirements:
|
87
73
|
- - ">="
|
@@ -94,7 +80,7 @@ dependencies:
|
|
94
80
|
- - ">="
|
95
81
|
- !ruby/object:Gem::Version
|
96
82
|
version: '0'
|
97
|
-
description:
|
83
|
+
description: RSpec Matchers for ActiveModel::Serializer Associations
|
98
84
|
email:
|
99
85
|
- tonyta.tt@gmail.com
|
100
86
|
executables: []
|
@@ -103,19 +89,21 @@ extra_rdoc_files: []
|
|
103
89
|
files:
|
104
90
|
- ".gitignore"
|
105
91
|
- ".rspec"
|
92
|
+
- ".travis.yml"
|
106
93
|
- Gemfile
|
107
94
|
- LICENSE.txt
|
108
95
|
- README.md
|
109
96
|
- Rakefile
|
110
97
|
- active_model_serializers_matchers.gemspec
|
111
98
|
- lib/active_model_serializers_matchers.rb
|
112
|
-
- lib/active_model_serializers_matchers/
|
113
|
-
- lib/active_model_serializers_matchers/have_one_association_matcher.rb
|
99
|
+
- lib/active_model_serializers_matchers/association_matcher.rb
|
114
100
|
- lib/active_model_serializers_matchers/version.rb
|
115
|
-
- spec/active_model_serializers_matchers/
|
116
|
-
- spec/active_model_serializers_matchers/have_one_association_matcher_spec.rb
|
101
|
+
- spec/active_model_serializers_matchers/association_matcher_spec.rb
|
117
102
|
- spec/active_model_serializers_matchers_spec.rb
|
118
103
|
- spec/spec_helper.rb
|
104
|
+
- spec/support/dummy_serializers.rb
|
105
|
+
- spec/support/rspec_fail_matchers.rb
|
106
|
+
- spec/support/shared_examples.rb
|
119
107
|
homepage: ''
|
120
108
|
licenses:
|
121
109
|
- MIT
|
@@ -128,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
116
|
requirements:
|
129
117
|
- - ">="
|
130
118
|
- !ruby/object:Gem::Version
|
131
|
-
version:
|
119
|
+
version: 1.9.3
|
132
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
121
|
requirements:
|
134
122
|
- - ">="
|
@@ -136,12 +124,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
124
|
version: '0'
|
137
125
|
requirements: []
|
138
126
|
rubyforge_project:
|
139
|
-
rubygems_version: 2.
|
127
|
+
rubygems_version: 2.2.2
|
140
128
|
signing_key:
|
141
129
|
specification_version: 4
|
142
|
-
summary:
|
130
|
+
summary: These matchers will allow you to test associations between serializers.
|
143
131
|
test_files:
|
144
|
-
- spec/active_model_serializers_matchers/
|
145
|
-
- spec/active_model_serializers_matchers/have_one_association_matcher_spec.rb
|
132
|
+
- spec/active_model_serializers_matchers/association_matcher_spec.rb
|
146
133
|
- spec/active_model_serializers_matchers_spec.rb
|
147
134
|
- spec/spec_helper.rb
|
135
|
+
- spec/support/dummy_serializers.rb
|
136
|
+
- spec/support/rspec_fail_matchers.rb
|
137
|
+
- spec/support/shared_examples.rb
|
@@ -1,83 +0,0 @@
|
|
1
|
-
module ActiveModelSerializersMatchers
|
2
|
-
class HaveManyAssociationMatcher
|
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 many #{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::HasMany)}"
|
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::HasMany)}"
|
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::HasMany
|
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
|
@@ -1,83 +0,0 @@
|
|
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
|