/
allow_value_matcher_spec.rb
673 lines (552 loc) · 21.2 KB
/
allow_value_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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveModel, type: :model do
describe '#allow_values' do
it 'is aliased to #allow_value' do
expect(method(:allow_values)).to eq(method(:allow_value))
end
end
end
describe Shoulda::Matchers::ActiveModel::AllowValueMatcher, type: :model do
context "#description" do
it 'describes itself with multiple values' do
matcher = allow_value('foo', 'bar').for(:baz)
expect(matcher.description).to eq(
'allow :baz to be ‹"foo"› or ‹"bar"›'
)
end
it 'describes itself with a single value' do
matcher = allow_value('foo').for(:baz)
expect(matcher.description).to eq 'allow :baz to be ‹"foo"›'
end
if active_model_3_2?
it 'describes itself with a strict validation' do
strict_matcher = allow_value('xyz').for(:attr).strict
expect(strict_matcher.description).to eq(
'allow :attr to be ‹"xyz"›, raising a validation exception on failure'
)
end
end
end
describe '#_after_setting_value' do
it 'sets a block which is yielded after each value is set on the attribute' do
attribute = :attr
record = define_model(:example, attribute => :string).new
matcher = described_class.new('a', 'b', 'c').for(attribute)
call_count = 0
matcher._after_setting_value { call_count += 1 }
matcher.matches?(record)
expect(call_count).to eq 3
end
end
context 'an attribute with a validation' do
it 'allows a good value' do
expect(validating_format(with: /abc/)).to allow_value('abcde').for(:attr)
end
it 'rejects a bad value with an appropriate failure message' do
message = <<-MESSAGE
After setting :attr to ‹"xyz"›, the matcher expected the Example to be
valid, but it was invalid instead, producing these validation errors:
* attr: ["is invalid"]
MESSAGE
assertion = lambda do
expect(validating_format(with: /abc/)).to allow_value('xyz').for(:attr)
end
expect(&assertion).to fail_with_message(message)
end
it 'allows several good values' do
expect(validating_format(with: /abc/)).
to allow_value('abcde', 'deabc').for(:attr)
end
context 'given several bad values' do
it 'rejects' do
expect(validating_format(with: /abc/)).
not_to allow_value('xyz', 'zyx', nil, []).
for(:attr).
ignoring_interference_by_writer
end
it 'produces an appropriate failure message' do
message = <<-MESSAGE
After setting :attr to ‹"zyx"›, the matcher expected the Example to be
valid, but it was invalid instead, producing these validation errors:
* attr: ["is invalid"]
MESSAGE
assertion = lambda do
expect(validating_format(with: /abc/)).
to allow_value('abc', 'abcde', 'zyx', nil, []).
for(:attr).
ignoring_interference_by_writer
end
expect(&assertion).to fail_with_message(message)
end
end
end
context 'an attribute with a validation and a custom message' do
it 'allows a good value' do
expect(validating_format(with: /abc/, message: 'bad value')).
to allow_value('abcde').for(:attr).with_message(/bad/)
end
it 'rejects a bad value with an appropriate failure message' do
message = <<-MESSAGE
After setting :attr to ‹"xyz"›, the matcher expected the Example to be
valid, but it was invalid instead, producing these validation errors:
* attr: ["bad value"]
MESSAGE
assertion = lambda do
expect(validating_format(with: /abc/, message: 'bad value')).
to allow_value('xyz').for(:attr).with_message(/bad/)
end
expect(&assertion).to fail_with_message(message)
end
context 'when the custom messages do not match' do
it 'rejects with an appropriate failure message' do
message = <<-MESSAGE
After setting :attr to ‹"xyz"›, the matcher expected the Example to be
invalid and to produce a validation error matching ‹/different/› on
:attr. The record was indeed invalid, but it produced these validation
errors instead:
* attr: ["bad value"]
MESSAGE
assertion = lambda do
expect(validating_format(with: /abc/, message: 'bad value')).
not_to allow_value('xyz').for(:attr).with_message(/different/)
end
expect(&assertion).to fail_with_message(message)
end
end
context 'when interpolation values are provided along with a custom message' do
context 'when the messages match' do
it 'accepts' do
options = {
attribute_name: :attr,
attribute_type: :string
}
record = record_with_custom_validation(options) do
if self.attr == 'xyz'
self.errors.add :attr, :greater_than, count: 2
end
end
expect(record).
not_to allow_value('xyz').
for(:attr).
with_message(:greater_than, values: { count: 2 })
end
end
context 'when the messages do not match' do
it 'rejects with an appropriate failure message' do
options = {
attribute_name: :attr,
attribute_type: :string
}
record = record_with_custom_validation(options) do
if self.attr == 'xyz'
self.errors.add :attr, "some other error"
end
end
assertion = lambda do
expect(record).
not_to allow_value('xyz').
for(:attr).
with_message(:greater_than, values: { count: 2 })
end
message = <<-MESSAGE
After setting :attr to ‹"xyz"›, the matcher expected the Example to be
invalid and to produce the validation error "must be greater than 2" on
:attr. The record was indeed invalid, but it produced these validation
errors instead:
* attr: ["some other error"]
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
end
context 'when the attribute being validated is different than the attribute that receives the validation error' do
include UnitTests::AllowValueMatcherHelpers
context 'when the validation error message was provided directly' do
context 'given a valid value' do
it 'accepts' do
builder = builder_for_record_with_different_error_attribute
expect(builder.record).
to allow_value(builder.valid_value).
for(builder.attribute_to_validate).
with_message(
builder.message,
against: builder.attribute_that_receives_error
)
end
end
context 'given an invalid value' do
it 'rejects' do
builder = builder_for_record_with_different_error_attribute
invalid_value = "#{builder.valid_value} (invalid)"
expect(builder.record).
not_to allow_value(invalid_value).
for(builder.attribute_to_validate).
with_message(
builder.message,
against: builder.attribute_that_receives_error
)
end
context 'if the messages do not match' do
it 'technically accepts' do
builder = builder_for_record_with_different_error_attribute(
message: "a different error"
)
invalid_value = "#{builder.valid_value} (invalid)"
assertion = lambda do
expect(builder.record).
not_to allow_value(invalid_value).
for(builder.attribute_to_validate).
with_message(
"some error",
against: builder.attribute_that_receives_error
)
end
message = <<-MESSAGE
After setting :#{builder.attribute_to_validate} to ‹"#{invalid_value}"›, the
matcher expected the #{builder.model.name} to be invalid and to produce the validation
error "some error" on :#{builder.attribute_that_receives_error}. The record was
indeed invalid, but it produced these validation errors instead:
* #{builder.attribute_that_receives_error}: ["a different error"]
MESSAGE
expect(&assertion).to fail_with_message(message)
end
end
end
end
context 'when the validation error message was provided via i18n' do
it 'passes given a valid value' do
builder = builder_for_record_with_different_error_attribute_using_i18n
expect(builder.record).
to allow_value(builder.valid_value).
for(builder.attribute_to_validate).
with_message(
builder.validation_message_key,
against: builder.attribute_that_receives_error
)
end
it 'fails given an invalid value' do
builder = builder_for_record_with_different_error_attribute_using_i18n
invalid_value = "#{builder.valid_value} (invalid)"
expect(builder.record).
not_to allow_value(invalid_value).
for(builder.attribute_to_validate).
with_message(
builder.validation_message_key,
against: builder.attribute_that_receives_error
)
end
end
end
context "an attribute with a context-dependent validation" do
context "without the validation context" do
it "allows a bad value" do
expect(validating_format(with: /abc/, on: :customisable)).to allow_value("xyz").for(:attr)
end
end
context "with the validation context" do
it "allows a good value" do
expect(validating_format(with: /abc/, on: :customisable)).to allow_value("abcde").for(:attr).on(:customisable)
end
it "rejects a bad value" do
expect(validating_format(with: /abc/, on: :customisable)).not_to allow_value("xyz").for(:attr).on(:customisable)
end
end
end
context 'an attribute with several validations' do
let(:model) do
define_model :example, attr: :string do
validates_presence_of :attr
validates_length_of :attr, within: 1..5
validates_numericality_of :attr, greater_than_or_equal_to: 1,
less_than_or_equal_to: 50000
end.new
end
bad_values = [nil, '', 'abc', '0', '50001', '123456', []]
it 'matches given a good value' do
expect(model).to allow_value('12345').for(:attr)
end
it 'does not match given a bad value' do
bad_values.each do |bad_value|
expect(model).
not_to allow_value(bad_value).
for(:attr).
ignoring_interference_by_writer
end
end
it 'does not match given multiple bad values' do
expect(model).
not_to allow_value(*bad_values).
for(:attr).
ignoring_interference_by_writer
end
it "does not match given good values along with bad values" do
message = <<-MESSAGE.strip_heredoc
After setting :attr to ‹"12345"›, the matcher expected the Example to be
invalid, but it was valid instead.
MESSAGE
assertion = lambda do
expect(model).not_to allow_value('12345', *bad_values).for(:attr)
end
expect(&assertion).to fail_with_message(message)
end
end
context 'with a single value' do
it 'allows you to call description before calling matches?' do
model = define_model(:example, attr: :string).new
matcher = described_class.new('foo').for(:attr)
matcher.description
expect { matcher.matches?(model) }.not_to raise_error
end
end
context 'with no values' do
it 'raises an error' do
expect { allow_value.for(:baz) }.
to raise_error(ArgumentError, /at least one argument/)
end
end
if active_model_3_2?
context 'an attribute with a strict format validation' do
context 'when qualified with strict' do
it 'rejects a bad value, providing the correct failure message' do
message = <<-MESSAGE.strip_heredoc
After setting :attr to ‹"xyz"›, the matcher expected the Example to be
valid, but it was invalid instead, raising a validation exception with
the message "Attr is invalid".
MESSAGE
assertion = lambda do
expect(validating_format(with: /abc/, strict: true)).
to allow_value('xyz').for(:attr).strict
end
expect(&assertion).to fail_with_message(message)
end
context 'qualified with a custom message' do
it 'rejects a bad value when the failure messages do not match' do
message = <<-MESSAGE.strip_heredoc
After setting :attr to ‹"xyz"›, the matcher expected the Example to be
invalid and to raise a validation exception with message matching
‹/abc/›. The record was indeed invalid, but the exception message was
"Attr is invalid" instead.
MESSAGE
assertion = lambda do
expect(validating_format(with: /abc/, strict: true)).
not_to allow_value('xyz').for(:attr).with_message(/abc/).strict
end
expect(&assertion).to fail_with_message(message)
end
end
end
end
end
context 'when the attribute interferes with attempts to be set' do
context 'when the attribute cannot be changed from nil to non-nil' do
context 'and the record remains valid' do
it 'accepts (and does not raise an AttributeChangedValueError)' do
model = define_active_model_class 'Example', accessors: [:name] do
def name=(_value)
nil
end
end
expect(model.new).to allow_value('anything').for(:name)
end
end
context 'and the record becomes invalid' do
it 'rejects with an appropriate failure message' do
model = define_active_model_class 'Example', accessors: [:name] do
validates_presence_of :name
def name=(_value)
nil
end
end
assertion = lambda do
expect(model.new).to allow_value('anything').for(:name)
end
message = <<-MESSAGE.strip
After setting :name to ‹"anything"› -- which was read back as ‹nil› --
the matcher expected the Example to be valid, but it was invalid
instead, producing these validation errors:
* name: ["can't be blank"]
As indicated in the message above, :name 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
expect(&assertion).to fail_with_message(message)
end
end
end
context 'when the attribute cannot be changed from non-nil to nil' do
context 'and the record remains valid' do
it 'accepts (and does not raise an AttributeChangedValueError)' do
model = define_active_model_class 'Example', accessors: [:name] do
def name=(value)
if value
super(value)
end
end
end
record = model.new(name: 'some name')
expect(record).to allow_value(nil).for(:name)
end
end
context 'and the record becomes invalid' do
it 'rejects with an appropriate failure message' do
model = define_active_model_class 'Example', accessors: [:name] do
validates_absence_of :name
def name=(value)
if value
super(value)
end
end
end
record = model.new(name: 'some name')
assertion = lambda do
expect(record).to allow_value(nil).for(:name)
end
message = <<-MESSAGE.strip
After setting :name to ‹nil› -- which was read back as ‹"some name"› --
the matcher expected the Example to be valid, but it was invalid
instead, producing these validation errors:
* name: ["must be blank"]
As indicated in the message above, :name 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
expect(&assertion).to fail_with_message(message)
end
end
end
context 'when the attribute cannot be changed from a non-nil value to another non-nil value' do
context 'and the record remains valid' do
it 'accepts (and does not raise an AttributeChangedValueError)' do
model = define_active_model_class 'Example', accessors: [:name] do
def name=(_value)
super('constant name')
end
end
record = model.new(name: 'some name')
expect(record).to allow_value('another name').for(:name)
end
end
context 'and the record becomes invalid' do
it 'rejects with an appropriate failure message' do
model = define_active_model_class 'Example', accessors: [:name] do
validates_format_of :name, with: /another name/
def name=(value)
super('constant name')
end
end
record = model.new(name: 'some name')
assertion = lambda do
expect(record).to allow_value('another name').for(:name)
end
message = <<-MESSAGE.strip
After setting :name to ‹"another name"› -- which was read back as
‹"constant name"› -- the matcher expected the Example to be valid, but
it was invalid instead, producing these validation errors:
* name: ["is invalid"]
As indicated in the message above, :name 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
expect(&assertion).to fail_with_message(message)
end
end
end
end
context 'when the attribute does not exist on the model' do
context 'when the assertion is positive' do
it 'raises an AttributeDoesNotExistError' do
model = define_class('Example')
assertion = lambda do
expect(model.new).to allow_value('foo').for(:nonexistent)
end
message = <<-MESSAGE.rstrip
The matcher attempted to set :nonexistent on the Example to "foo", but
that attribute does not exist.
MESSAGE
expect(&assertion).to raise_error(
described_class::AttributeDoesNotExistError,
message
)
end
end
context 'when the assertion is negative' do
it 'raises an AttributeDoesNotExistError' do
model = define_class('Example')
assertion = lambda do
expect(model.new).not_to allow_value('foo').for(:nonexistent)
end
message = <<-MESSAGE.rstrip
The matcher attempted to set :nonexistent on the Example to "foo", but
that attribute does not exist.
MESSAGE
expect(&assertion).to raise_error(
described_class::AttributeDoesNotExistError,
message
)
end
end
end
context 'given attributes to preset on the record before validation' do
context 'when the assertion is positive' do
context 'if any attributes do not exist on the model' do
it 'raises an AttributeDoesNotExistError' do
model = define_active_model_class('Example', accessors: [:existent])
allow_value_matcher = allow_value('foo').for(:existent).tap do |matcher|
matcher.values_to_preset = { nonexistent: 'some value' }
end
assertion = lambda do
expect(model.new).to(allow_value_matcher)
end
message = <<-MESSAGE.rstrip
The matcher attempted to set :nonexistent on the Example to "some
value", but that attribute does not exist.
MESSAGE
expect(&assertion).to raise_error(
described_class::AttributeDoesNotExistError,
message
)
end
end
end
context 'when the assertion is negative' do
context 'if any attributes do not exist on the model' do
it 'raises an AttributeDoesNotExistError' do
model = define_active_model_class('Example', accessors: [:existent])
allow_value_matcher = allow_value('foo').for(:existent).tap do |matcher|
matcher.values_to_preset = { nonexistent: 'some value' }
end
assertion = lambda do
expect(model.new).not_to(allow_value_matcher)
end
message = <<-MESSAGE.rstrip
The matcher attempted to set :nonexistent on the Example to "some
value", but that attribute does not exist.
MESSAGE
expect(&assertion).to raise_error(
described_class::AttributeDoesNotExistError,
message
)
end
end
end
end
if active_record_supports_enum?
context 'given an ActiveRecord model' do
context 'where the attribute under test is an enum and the given value is a value in that enum' do
it 'accepts' do
model = define_model('Shipment', status: :integer) do
enum status: { pending: 1, shipped: 2, delivered: 3 }
end
expect(model.new).to allow_value(1).for(:status)
end
end
end
end
end