/
validate_length_of_matcher_spec.rb
391 lines (335 loc) · 12.9 KB
/
validate_length_of_matcher_spec.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveModel::ValidateLengthOfMatcher, type: :model do
context 'an attribute with a non-zero minimum length validation' do
it 'accepts ensuring the correct minimum length' do
expect(validating_length(minimum: 4)).
to validate_length_of(:attr).is_at_least(4)
end
it 'rejects ensuring a lower minimum length with any message' do
expect(validating_length(minimum: 4)).
not_to validate_length_of(:attr).is_at_least(3).with_short_message(/.*/)
end
it 'rejects ensuring a higher minimum length with any message' do
expect(validating_length(minimum: 4)).
not_to validate_length_of(:attr).is_at_least(5).with_short_message(/.*/)
end
it 'does not override the default message with a blank' do
expect(validating_length(minimum: 4)).
to validate_length_of(:attr).is_at_least(4).with_short_message(nil)
end
it_supports(
'ignoring_interference_by_writer',
tests: {
accept_if_qualified_but_changing_value_does_not_interfere: {
changing_values_with: :upcase,
},
reject_if_qualified_but_changing_value_interferes: {
model_name: 'Example',
attribute_name: :attr,
changing_values_with: :add_character,
expected_message: <<-MESSAGE.strip
Expected Example to validate that the length of :attr is at least 4, but
this could not be proved.
After setting :attr to ‹"xxx"› -- which was read back as ‹"xxxa"› --
the matcher expected the Example to be invalid, but it was valid
instead.
As indicated in the message above, :attr seems to be changing certain
values as they are set, and this could have something to do with why
this test is failing. If you've overridden the writer method for this
attribute, then you may need to change it to make this test pass, or
do something else entirely.
MESSAGE
}
}
) do
def validation_matcher_scenario_args
super.deep_merge(validation_options: { minimum: 4 })
end
def configure_validation_matcher(matcher)
matcher.is_at_least(4)
end
end
it 'fails when used in the negative' do
assertion = lambda do
expect(validating_length(minimum: 4)).
not_to validate_length_of(:attr).is_at_least(4)
end
message = <<-MESSAGE
Expected Example not to validate that the length of :attr is at least 4,
but this could not be proved.
After setting :attr to ‹"xxxx"›, the matcher expected the Example to
be invalid, but it was valid instead.
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
context 'an attribute with a minimum length validation of 0' do
it 'accepts ensuring the correct minimum length' do
expect(validating_length(minimum: 0)).
to validate_length_of(:attr).is_at_least(0)
end
end
context 'an attribute with a maximum length' do
it 'accepts ensuring the correct maximum length' do
expect(validating_length(maximum: 4)).
to validate_length_of(:attr).is_at_most(4)
end
it 'rejects ensuring a lower maximum length with any message' do
expect(validating_length(maximum: 4)).
not_to validate_length_of(:attr).is_at_most(3).with_long_message(/.*/)
end
it 'rejects ensuring a higher maximum length with any message' do
expect(validating_length(maximum: 4)).
not_to validate_length_of(:attr).is_at_most(5).with_long_message(/.*/)
end
it 'does not override the default message with a blank' do
expect(validating_length(maximum: 4)).
to validate_length_of(:attr).is_at_most(4).with_long_message(nil)
end
it_supports(
'ignoring_interference_by_writer',
tests: {
accept_if_qualified_but_changing_value_does_not_interfere: {
changing_values_with: :upcase,
},
reject_if_qualified_but_changing_value_interferes: {
model_name: 'Example',
attribute_name: :attr,
changing_values_with: :remove_character,
expected_message: <<-MESSAGE.strip
Expected Example to validate that the length of :attr is at most 4, but
this could not be proved.
After setting :attr to ‹"xxxxx"› -- which was read back as ‹"xxxx"› --
the matcher expected the Example to be invalid, but it was valid
instead.
As indicated in the message above, :attr seems to be changing certain
values as they are set, and this could have something to do with why
this test is failing. If you've overridden the writer method for this
attribute, then you may need to change it to make this test pass, or
do something else entirely.
MESSAGE
}
}
) do
def validation_matcher_scenario_args
super.deep_merge(validation_options: { maximum: 4 })
end
def configure_validation_matcher(matcher)
matcher.is_at_most(4)
end
end
end
context 'an attribute with a required exact length' do
it 'accepts ensuring the correct length' do
expect(validating_length(is: 4)).
to validate_length_of(:attr).is_equal_to(4)
end
it 'rejects ensuring a lower maximum length with any message' do
expect(validating_length(is: 4)).
not_to validate_length_of(:attr).is_equal_to(3).with_message(/.*/)
end
it 'rejects ensuring a higher maximum length with any message' do
expect(validating_length(is: 4)).
not_to validate_length_of(:attr).is_equal_to(5).with_message(/.*/)
end
it 'does not override the default message with a blank' do
expect(validating_length(is: 4)).
to validate_length_of(:attr).is_equal_to(4).with_message(nil)
end
it_supports(
'ignoring_interference_by_writer',
tests: {
accept_if_qualified_but_changing_value_does_not_interfere: {
changing_values_with: :upcase,
},
reject_if_qualified_but_changing_value_interferes: {
model_name: 'Example',
attribute_name: :attr,
changing_values_with: :add_character,
expected_message: <<-MESSAGE.strip
Expected Example to validate that the length of :attr is 4, but this
could not be proved.
After setting :attr to ‹"xxx"› -- which was read back as ‹"xxxa"› --
the matcher expected the Example to be invalid, but it was valid
instead.
As indicated in the message above, :attr seems to be changing certain
values as they are set, and this could have something to do with why
this test is failing. If you've overridden the writer method for this
attribute, then you may need to change it to make this test pass, or
do something else entirely.
MESSAGE
}
}
) do
def validation_matcher_scenario_args
super.deep_merge(validation_options: { is: 4 })
end
def configure_validation_matcher(matcher)
matcher.is_equal_to(4)
end
end
end
context 'an attribute with a required exact length and another validation' do
it 'accepts ensuring the correct length' do
model = define_model(:example, attr: :string) do
validates_length_of :attr, is: 4
validates_numericality_of :attr
end.new
expect(model).to validate_length_of(:attr).is_equal_to(4)
end
end
context 'an attribute with a custom minimum length validation' do
it 'accepts ensuring the correct minimum length' do
expect(validating_length(minimum: 4, too_short: 'foobar')).
to validate_length_of(:attr).is_at_least(4).with_short_message(/foo/)
end
end
context 'an attribute with a custom maximum length validation' do
it 'accepts ensuring the correct minimum length' do
expect(validating_length(maximum: 4, too_long: 'foobar')).
to validate_length_of(:attr).is_at_most(4).with_long_message(/foo/)
end
end
context 'an attribute with a custom equal validation' do
it 'accepts ensuring the correct exact length' do
expect(validating_length(is: 4, message: 'foobar')).
to validate_length_of(:attr).is_equal_to(4).with_message(/foo/)
end
end
context 'an attribute without a length validation' do
it 'rejects ensuring a minimum length' do
expect(define_model(:example, attr: :string).new).
not_to validate_length_of(:attr).is_at_least(1)
end
end
context 'using translations' do
after { I18n.backend.reload! }
context "a too_long translation containing %{attribute}, %{model}" do
before do
stub_translation(
"activerecord.errors.messages.too_long",
"The %{attribute} of your %{model} is too long (maximum is %{count} characters)")
end
it "does not raise an exception" do
expect {
expect(validating_length(maximum: 4)).
to validate_length_of(:attr).is_at_most(4)
}.to_not raise_exception
end
end
context "a too_short translation containing %{attribute}, %{model}" do
before do
stub_translation(
"activerecord.errors.messages.too_short",
"The %{attribute} of your %{model} is too short (minimum is %{count} characters)")
end
it "does not raise an exception" do
expect {
expect(validating_length(minimum: 4)).to validate_length_of(:attr).is_at_least(4)
}.to_not raise_exception
end
end
context "a wrong_length translation containing %{attribute}, %{model}" do
before do
stub_translation(
"activerecord.errors.messages.wrong_length",
"The %{attribute} of your %{model} is the wrong length (should be %{count} characters)")
end
it "does not raise an exception" do
expect {
expect(validating_length(is: 4)).
to validate_length_of(:attr).is_equal_to(4)
}.to_not raise_exception
end
end
end
context 'qualified with allow_nil' do
context 'and validating with allow_nil' do
it 'accepts' do
expect(validating_length(minimum: 1, allow_nil: true)).
to validate_length_of(:attr).is_at_least(1).allow_nil
end
end
context 'and not validating with allow_nil' do
it 'rejects' do
assertion = lambda do
expect(validating_length(minimum: 1)).
to validate_length_of(:attr).is_at_least(1).allow_nil
end
message = <<-MESSAGE
Expected Example to validate that the length of :attr is at least 1, but
this could not be proved.
After setting :attr to ‹nil›, the matcher expected the Example to be
valid, but it was invalid instead, producing these validation errors:
* attr: ["is too short (minimum is 1 character)"]
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
context 'qualified with allow_blank' do
context 'and validating with allow_blank' do
context 'with minimum' do
context 'and minimum is 1' do
it 'accepts' do
expect(validating_length(minimum: 1, allow_blank: true)).
to validate_length_of(:attr).is_at_least(1).allow_blank
end
end
context 'and minimum is greater than 1' do
it 'accepts' do
expect(validating_length(minimum: 2, allow_blank: true)).
to validate_length_of(:attr).is_at_least(2).allow_blank
end
end
end
context 'with maximum' do
context 'and maximum is 0' do
it 'accepts' do
expect(validating_length(maximum: 0, allow_blank: true)).
to validate_length_of(:attr).is_at_most(0).allow_blank
end
end
context 'and minimum is greater than 0' do
it 'accepts' do
expect(validating_length(maximum: 1, allow_blank: true)).
to validate_length_of(:attr).is_at_most(1).allow_blank
end
end
end
end
context 'and not validating with allow_blank' do
it 'rejects' do
assertion = lambda do
expect(validating_length(minimum: 1)).
to validate_length_of(:attr).is_at_least(1).allow_blank
end
message = <<-MESSAGE
Expected Example to validate that the length of :attr is at least 1, but
this could not be proved.
After setting :attr to ‹""›, the matcher expected the Example to be
valid, but it was invalid instead, producing these validation errors:
* attr: ["is too short (minimum is 1 character)"]
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
def define_model_validating_length(options = {})
options = options.dup
attribute_name = options.delete(:attribute_name) { :attr }
define_model(:example, attribute_name => :string) do |model|
model.validates_length_of(attribute_name, options)
end
end
def validating_length(options = {})
define_model_validating_length(options).new
end
alias_method :build_record_validating_length, :validating_length
def validation_matcher_scenario_args
super.deep_merge(
matcher_name: :validate_length_of,
model_creator: :active_model
)
end
end