-
-
Notifications
You must be signed in to change notification settings - Fork 912
/
accept_nested_attributes_for_matcher.rb
217 lines (198 loc) · 5.75 KB
/
accept_nested_attributes_for_matcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
module Shoulda
module Matchers
module ActiveRecord
# The `accept_nested_attributes_for` matcher tests usage of the
# `accepts_nested_attributes_for` macro.
#
# class Car < ActiveRecord::Base
# accepts_nested_attributes_for :doors
# end
#
# # RSpec
# RSpec.describe Car, type: :model do
# it { should accept_nested_attributes_for(:doors) }
# end
#
# # Minitest (Shoulda) (using Shoulda)
# class CarTest < ActiveSupport::TestCase
# should accept_nested_attributes_for(:doors)
# end
#
# #### Qualifiers
#
# ##### allow_destroy
#
# Use `allow_destroy` to assert that the `:allow_destroy` option was
# specified.
#
# class Car < ActiveRecord::Base
# accepts_nested_attributes_for :mirrors, allow_destroy: true
# end
#
# # RSpec
# RSpec.describe Car, type: :model do
# it do
# should accept_nested_attributes_for(:mirrors).
# allow_destroy(true)
# end
# end
#
# # Minitest (Shoulda)
# class CarTest < ActiveSupport::TestCase
# should accept_nested_attributes_for(:mirrors).
# allow_destroy(true)
# end
#
# ##### limit
#
# Use `limit` to assert that the `:limit` option was specified.
#
# class Car < ActiveRecord::Base
# accepts_nested_attributes_for :windows, limit: 3
# end
#
# # RSpec
# RSpec.describe Car, type: :model do
# it do
# should accept_nested_attributes_for(:windows).
# limit(3)
# end
# end
#
# # Minitest (Shoulda)
# class CarTest < ActiveSupport::TestCase
# should accept_nested_attributes_for(:windows).
# limit(3)
# end
#
# ##### update_only
#
# Use `update_only` to assert that the `:update_only` option was
# specified.
#
# class Car < ActiveRecord::Base
# accepts_nested_attributes_for :engine, update_only: true
# end
#
# # RSpec
# RSpec.describe Car, type: :model do
# it do
# should accept_nested_attributes_for(:engine).
# update_only(true)
# end
# end
#
# # Minitest (Shoulda)
# class CarTest < ActiveSupport::TestCase
# should accept_nested_attributes_for(:engine).
# update_only(true)
# end
#
# @return [AcceptNestedAttributesForMatcher]
#
def accept_nested_attributes_for(name)
AcceptNestedAttributesForMatcher.new(name)
end
# @private
class AcceptNestedAttributesForMatcher
def initialize(name)
@name = name
@options = {}
end
def allow_destroy(allow_destroy)
@options[:allow_destroy] = allow_destroy
self
end
def limit(limit)
@options[:limit] = limit
self
end
def update_only(update_only)
@options[:update_only] = update_only
self
end
def matches?(subject)
@subject = subject
exists? &&
allow_destroy_correct? &&
limit_correct? &&
update_only_correct?
end
def failure_message
"Expected #{expectation} (#{@problem})"
end
def failure_message_when_negated
"Did not expect #{expectation}"
end
def description
description = "accepts_nested_attributes_for :#{@name}"
if @options.key?(:allow_destroy)
description += " allow_destroy => #{@options[:allow_destroy]}"
end
if @options.key?(:limit)
description += " limit => #{@options[:limit]}"
end
if @options.key?(:update_only)
description += " update_only => #{@options[:update_only]}"
end
description
end
protected
def exists?
if config
true
else
@problem = 'is not declared'
false
end
end
def allow_destroy_correct?
failure_message = "#{should_or_should_not(@options[:allow_destroy])}"\
' allow destroy'
verify_option_is_correct(:allow_destroy, failure_message)
end
def limit_correct?
failure_message = "limit should be #{@options[:limit]},"\
" got #{config[:limit]}"
verify_option_is_correct(:limit, failure_message)
end
def update_only_correct?
failure_message = "#{should_or_should_not(@options[:update_only])}"\
' be update only'
verify_option_is_correct(:update_only, failure_message)
end
def verify_option_is_correct(option, failure_message)
if @options.key?(option)
if @options[option] == config[option]
true
else
@problem = failure_message
false
end
else
true
end
end
def config
model_config[@name]
end
def model_config
model_class.nested_attributes_options
end
def model_class
@subject.class
end
def expectation
"#{model_class.name} to accept nested attributes for #{@name}"
end
def should_or_should_not(value)
if value
'should'
else
'should not'
end
end
end
end
end
end