shoulda-matchers 2.8.0.rc1 → 2.8.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.hound.yml +3 -0
- data/.hound_config/ruby.yml +5 -0
- data/.travis.yml +4 -0
- data/.yardopts +1 -1
- data/Appraisals +5 -2
- data/Gemfile +1 -1
- data/Gemfile.lock +7 -5
- data/NEWS.md +24 -0
- data/README.md +9 -0
- data/gemfiles/3.0.gemfile +1 -1
- data/gemfiles/3.0.gemfile.lock +7 -5
- data/gemfiles/3.1.gemfile +1 -1
- data/gemfiles/3.1.gemfile.lock +7 -5
- data/gemfiles/3.1_1.9.2.gemfile +1 -3
- data/gemfiles/3.1_1.9.2.gemfile.lock +7 -12
- data/gemfiles/3.2.gemfile +1 -1
- data/gemfiles/3.2.gemfile.lock +7 -5
- data/gemfiles/3.2_1.9.2.gemfile +1 -3
- data/gemfiles/3.2_1.9.2.gemfile.lock +5 -10
- data/gemfiles/4.0.0.gemfile +1 -1
- data/gemfiles/4.0.0.gemfile.lock +7 -5
- data/gemfiles/4.0.1.gemfile +1 -1
- data/gemfiles/4.0.1.gemfile.lock +7 -5
- data/gemfiles/4.1.gemfile +1 -1
- data/gemfiles/4.1.gemfile.lock +7 -5
- data/gemfiles/4.2.gemfile +5 -3
- data/gemfiles/4.2.gemfile.lock +26 -8
- data/lib/shoulda/matchers/action_controller/set_flash_matcher.rb +2 -0
- data/lib/shoulda/matchers/active_model.rb +3 -2
- data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +44 -32
- data/lib/shoulda/matchers/active_model/helpers.rb +8 -22
- data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +66 -19
- data/lib/shoulda/matchers/active_model/strict_validator.rb +51 -0
- data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +14 -13
- data/lib/shoulda/matchers/active_model/validator.rb +113 -0
- data/lib/shoulda/matchers/active_model/validator_with_captured_range_error.rb +12 -0
- data/lib/shoulda/matchers/active_record/association_matcher.rb +19 -1
- data/lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb +23 -2
- data/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb +14 -11
- data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +1 -0
- data/lib/shoulda/matchers/active_record/uniqueness/model.rb +1 -0
- data/lib/shoulda/matchers/active_record/uniqueness/namespace.rb +1 -0
- data/lib/shoulda/matchers/active_record/uniqueness/test_model_creator.rb +1 -0
- data/lib/shoulda/matchers/active_record/uniqueness/test_models.rb +1 -0
- data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +22 -15
- data/lib/shoulda/matchers/matcher_context.rb +1 -0
- data/lib/shoulda/matchers/rails_shim.rb +22 -0
- data/lib/shoulda/matchers/util.rb +5 -0
- data/lib/shoulda/matchers/version.rb +1 -1
- data/script/update_gem_in_all_appraisals +15 -0
- data/shoulda-matchers.gemspec +1 -1
- data/spec/support/unit/helpers/active_model_helpers.rb +3 -1
- data/spec/support/unit/helpers/active_model_versions.rb +8 -0
- data/spec/support/unit/helpers/active_record_versions.rb +16 -0
- data/spec/support/unit/helpers/rails_versions.rb +4 -4
- data/spec/support/unit/matchers/fail_with_message_matcher.rb +9 -8
- data/spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb +74 -0
- data/spec/unit/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +75 -0
- data/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb +24 -0
- data/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +78 -5
- data/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +31 -2
- data/spec/unit_spec_helper.rb +2 -0
- metadata +11 -8
- data/lib/shoulda/matchers/active_model/exception_message_finder.rb +0 -58
- data/lib/shoulda/matchers/active_model/validation_message_finder.rb +0 -69
- data/spec/unit/shoulda/matchers/active_model/exception_message_finder_spec.rb +0 -111
- data/spec/unit/shoulda/matchers/active_model/validation_message_finder_spec.rb +0 -129
@@ -68,6 +68,30 @@ describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :mode
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
+
context 'against an attribute with a specific column limit' do
|
72
|
+
it 'does not raise an exception when attempting to use the matcher' do
|
73
|
+
possible_values = (1..5).to_a
|
74
|
+
builder = build_object_allowing(possible_values)
|
75
|
+
assertion = -> { expect_to_match_on_values(builder, possible_values) }
|
76
|
+
expect(&assertion).not_to raise_error
|
77
|
+
end
|
78
|
+
|
79
|
+
def build_object(options = {}, &block)
|
80
|
+
build_object_with_generic_attribute(
|
81
|
+
options.merge(
|
82
|
+
column_type: :integer,
|
83
|
+
column_options: { limit: 2 },
|
84
|
+
value: 1
|
85
|
+
),
|
86
|
+
&block
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
90
|
+
def expect_to_match_on_values(builder, values, &block)
|
91
|
+
expect_to_match_in_array(builder, values, &block)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
71
95
|
context "against a float attribute" do
|
72
96
|
it_behaves_like 'it supports in_array',
|
73
97
|
possible_values: [1.0, 2.0, 3.0, 4.0, 5.0],
|
@@ -18,11 +18,11 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m
|
|
18
18
|
|
19
19
|
it 'rejects with the ActiveRecord :not_a_number message' do
|
20
20
|
the_matcher = matcher
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
expect do
|
22
|
+
expect(not_validating_numericality).to the_matcher
|
23
|
+
end.to fail_with_message_including(
|
24
|
+
'Expected errors to include "is not a number"'
|
25
|
+
)
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'rejects with the ActiveRecord :not_an_integer message' do
|
@@ -123,6 +123,72 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m
|
|
123
123
|
end
|
124
124
|
end
|
125
125
|
|
126
|
+
context 'qualified with less_than_or_equal_to' do
|
127
|
+
it 'does not raise an error if the given value is right at the allowed max value for the column' do
|
128
|
+
record = record_with_integer_column_of_limit(:attr, 2, less_than_or_equal_to: 32767)
|
129
|
+
assertion = -> {
|
130
|
+
expect(record).to validate_numericality_of(:attr).is_less_than_or_equal_to(32767)
|
131
|
+
}
|
132
|
+
expect(&assertion).not_to raise_error
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'qualified with less_than' do
|
137
|
+
it 'does not raise an error if the given value is right at the allowed max value for the column' do
|
138
|
+
record = record_with_integer_column_of_limit(:attr, 2, less_than: 32767)
|
139
|
+
assertion = -> {
|
140
|
+
expect(record).to validate_numericality_of(:attr).is_less_than(32767)
|
141
|
+
}
|
142
|
+
expect(&assertion).not_to raise_error
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'qualified with equal_to' do
|
147
|
+
it 'does not raise an error if the given value is right at the allowed min value for the column' do
|
148
|
+
record = record_with_integer_column_of_limit(:attr, 2, equal_to: -32768)
|
149
|
+
assertion = -> {
|
150
|
+
expect(record).to validate_numericality_of(:attr).is_equal_to(-32768)
|
151
|
+
}
|
152
|
+
expect(&assertion).not_to raise_error
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'does not raise an error if the given value is right at the allowed max value for the column' do
|
156
|
+
record = record_with_integer_column_of_limit(:attr, 2, equal_to: 32767)
|
157
|
+
assertion = -> {
|
158
|
+
expect(record).to validate_numericality_of(:attr).is_equal_to(32767)
|
159
|
+
}
|
160
|
+
expect(&assertion).not_to raise_error
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context 'qualified with greater_than_or_equal to' do
|
165
|
+
it 'does not raise an error if the given value is right at the allowed min value for the column' do
|
166
|
+
record = record_with_integer_column_of_limit(:attr, 2,
|
167
|
+
greater_than_or_equal_to: -32768
|
168
|
+
)
|
169
|
+
assertion = -> {
|
170
|
+
expect(record).
|
171
|
+
to validate_numericality_of(:attr).
|
172
|
+
is_greater_than_or_equal_to(-32768)
|
173
|
+
}
|
174
|
+
expect(&assertion).not_to raise_error
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
context 'qualified with greater_than' do
|
179
|
+
it 'does not raise an error if the given value is right at the allowed min value for the column' do
|
180
|
+
record = record_with_integer_column_of_limit(:attr, 2,
|
181
|
+
greater_than: -32768
|
182
|
+
)
|
183
|
+
assertion = -> {
|
184
|
+
expect(record).
|
185
|
+
to validate_numericality_of(:attr).
|
186
|
+
is_greater_than(-32768)
|
187
|
+
}
|
188
|
+
expect(&assertion).not_to raise_error
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
126
192
|
context 'with multiple options together' do
|
127
193
|
context 'the success cases' do
|
128
194
|
it do
|
@@ -349,4 +415,11 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :m
|
|
349
415
|
def matcher
|
350
416
|
validate_numericality_of(:attr)
|
351
417
|
end
|
418
|
+
|
419
|
+
def record_with_integer_column_of_limit(attribute, limit, validation_options = {})
|
420
|
+
column_options = { type: :integer, options: { limit: limit } }
|
421
|
+
define_model :example, attribute => column_options do
|
422
|
+
validates_numericality_of attribute, validation_options
|
423
|
+
end.new
|
424
|
+
end
|
352
425
|
end
|
@@ -646,8 +646,28 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher, type: :model do
|
|
646
646
|
end
|
647
647
|
|
648
648
|
it 'accepts an association with a valid :dependent option' do
|
649
|
-
|
650
|
-
|
649
|
+
dependent_options.each do |option|
|
650
|
+
expect(having_one_detail(dependent: option)).
|
651
|
+
to have_one(:detail).dependent(option)
|
652
|
+
end
|
653
|
+
end
|
654
|
+
|
655
|
+
it 'accepts any dependent option if true' do
|
656
|
+
dependent_options.each do |option|
|
657
|
+
expect(having_one_detail(dependent: option)).
|
658
|
+
to have_one(:detail).dependent(true)
|
659
|
+
end
|
660
|
+
end
|
661
|
+
|
662
|
+
it 'rejects any dependent options if false' do
|
663
|
+
dependent_options.each do |option|
|
664
|
+
expect(having_one_detail(dependent: option)).
|
665
|
+
to_not have_one(:detail).dependent(false)
|
666
|
+
end
|
667
|
+
end
|
668
|
+
|
669
|
+
it 'accepts a nil dependent option if false' do
|
670
|
+
expect(having_one_detail).to have_one(:detail).dependent(false)
|
651
671
|
end
|
652
672
|
|
653
673
|
it 'rejects an association with a bad :dependent option' do
|
@@ -1117,4 +1137,13 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher, type: :model do
|
|
1117
1137
|
args << options
|
1118
1138
|
model.__send__(macro, name, *args)
|
1119
1139
|
end
|
1140
|
+
|
1141
|
+
def dependent_options
|
1142
|
+
case Rails.version
|
1143
|
+
when /\A3/
|
1144
|
+
[:destroy, :delete, :nullify, :restrict]
|
1145
|
+
when /\A4/
|
1146
|
+
[:destroy, :delete, :nullify, :restrict_with_exception, :restrict_with_error]
|
1147
|
+
end
|
1148
|
+
end
|
1120
1149
|
end
|
data/spec/unit_spec_helper.rb
CHANGED
@@ -58,6 +58,8 @@ RSpec.configure do |config|
|
|
58
58
|
UnitTests::MailerBuilder.configure_example_group(config)
|
59
59
|
UnitTests::ModelBuilder.configure_example_group(config)
|
60
60
|
UnitTests::RailsVersions.configure_example_group(config)
|
61
|
+
UnitTests::ActiveRecordVersions.configure_example_group(config)
|
62
|
+
UnitTests::ActiveModelVersions.configure_example_group(config)
|
61
63
|
|
62
64
|
config.include UnitTests::Matchers
|
63
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shoulda-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.8.0.
|
4
|
+
version: 2.8.0.rc2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tammer Saleh
|
@@ -10,10 +10,11 @@ authors:
|
|
10
10
|
- Dan Croak
|
11
11
|
- Matt Jankowski
|
12
12
|
- Stafford Brunk
|
13
|
+
- Elliot Winkler
|
13
14
|
autorequire:
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
|
-
date:
|
17
|
+
date: 2015-01-23 00:00:00.000000000 Z
|
17
18
|
dependencies:
|
18
19
|
- !ruby/object:Gem::Dependency
|
19
20
|
name: activesupport
|
@@ -36,6 +37,8 @@ extensions: []
|
|
36
37
|
extra_rdoc_files: []
|
37
38
|
files:
|
38
39
|
- ".gitignore"
|
40
|
+
- ".hound.yml"
|
41
|
+
- ".hound_config/ruby.yml"
|
39
42
|
- ".travis.yml"
|
40
43
|
- ".yardopts"
|
41
44
|
- Appraisals
|
@@ -108,7 +111,6 @@ files:
|
|
108
111
|
- lib/shoulda/matchers/active_model/allow_value_matcher.rb
|
109
112
|
- lib/shoulda/matchers/active_model/disallow_value_matcher.rb
|
110
113
|
- lib/shoulda/matchers/active_model/errors.rb
|
111
|
-
- lib/shoulda/matchers/active_model/exception_message_finder.rb
|
112
114
|
- lib/shoulda/matchers/active_model/have_secure_password_matcher.rb
|
113
115
|
- lib/shoulda/matchers/active_model/helpers.rb
|
114
116
|
- lib/shoulda/matchers/active_model/numericality_matchers.rb
|
@@ -117,6 +119,7 @@ files:
|
|
117
119
|
- lib/shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher.rb
|
118
120
|
- lib/shoulda/matchers/active_model/numericality_matchers/odd_number_matcher.rb
|
119
121
|
- lib/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher.rb
|
122
|
+
- lib/shoulda/matchers/active_model/strict_validator.rb
|
120
123
|
- lib/shoulda/matchers/active_model/validate_absence_of_matcher.rb
|
121
124
|
- lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb
|
122
125
|
- lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
|
@@ -126,7 +129,8 @@ files:
|
|
126
129
|
- lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb
|
127
130
|
- lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb
|
128
131
|
- lib/shoulda/matchers/active_model/validation_matcher.rb
|
129
|
-
- lib/shoulda/matchers/active_model/
|
132
|
+
- lib/shoulda/matchers/active_model/validator.rb
|
133
|
+
- lib/shoulda/matchers/active_model/validator_with_captured_range_error.rb
|
130
134
|
- lib/shoulda/matchers/active_record.rb
|
131
135
|
- lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb
|
132
136
|
- lib/shoulda/matchers/active_record/association_matcher.rb
|
@@ -178,6 +182,7 @@ files:
|
|
178
182
|
- script/SUPPORTED_VERSIONS
|
179
183
|
- script/install_gems_in_all_appraisals
|
180
184
|
- script/run_all_tests
|
185
|
+
- script/update_gem_in_all_appraisals
|
181
186
|
- shoulda-matchers.gemspec
|
182
187
|
- spec/acceptance/active_model_integration_spec.rb
|
183
188
|
- spec/acceptance/independent_matchers_spec.rb
|
@@ -207,6 +212,7 @@ files:
|
|
207
212
|
- spec/support/unit/capture.rb
|
208
213
|
- spec/support/unit/helpers/active_model_helpers.rb
|
209
214
|
- spec/support/unit/helpers/active_model_versions.rb
|
215
|
+
- spec/support/unit/helpers/active_record_versions.rb
|
210
216
|
- spec/support/unit/helpers/active_resource_builder.rb
|
211
217
|
- spec/support/unit/helpers/allow_value_matcher_helpers.rb
|
212
218
|
- spec/support/unit/helpers/class_builder.rb
|
@@ -242,7 +248,6 @@ files:
|
|
242
248
|
- spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb
|
243
249
|
- spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb
|
244
250
|
- spec/unit/shoulda/matchers/active_model/disallow_value_matcher_spec.rb
|
245
|
-
- spec/unit/shoulda/matchers/active_model/exception_message_finder_spec.rb
|
246
251
|
- spec/unit/shoulda/matchers/active_model/have_secure_password_matcher_spec.rb
|
247
252
|
- spec/unit/shoulda/matchers/active_model/helpers_spec.rb
|
248
253
|
- spec/unit/shoulda/matchers/active_model/numericality_matchers/comparison_matcher_spec.rb
|
@@ -258,7 +263,6 @@ files:
|
|
258
263
|
- spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb
|
259
264
|
- spec/unit/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb
|
260
265
|
- spec/unit/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb
|
261
|
-
- spec/unit/shoulda/matchers/active_model/validation_message_finder_spec.rb
|
262
266
|
- spec/unit/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb
|
263
267
|
- spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb
|
264
268
|
- spec/unit/shoulda/matchers/active_record/association_matchers/model_reflection_spec.rb
|
@@ -336,6 +340,7 @@ test_files:
|
|
336
340
|
- spec/support/unit/capture.rb
|
337
341
|
- spec/support/unit/helpers/active_model_helpers.rb
|
338
342
|
- spec/support/unit/helpers/active_model_versions.rb
|
343
|
+
- spec/support/unit/helpers/active_record_versions.rb
|
339
344
|
- spec/support/unit/helpers/active_resource_builder.rb
|
340
345
|
- spec/support/unit/helpers/allow_value_matcher_helpers.rb
|
341
346
|
- spec/support/unit/helpers/class_builder.rb
|
@@ -371,7 +376,6 @@ test_files:
|
|
371
376
|
- spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb
|
372
377
|
- spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb
|
373
378
|
- spec/unit/shoulda/matchers/active_model/disallow_value_matcher_spec.rb
|
374
|
-
- spec/unit/shoulda/matchers/active_model/exception_message_finder_spec.rb
|
375
379
|
- spec/unit/shoulda/matchers/active_model/have_secure_password_matcher_spec.rb
|
376
380
|
- spec/unit/shoulda/matchers/active_model/helpers_spec.rb
|
377
381
|
- spec/unit/shoulda/matchers/active_model/numericality_matchers/comparison_matcher_spec.rb
|
@@ -387,7 +391,6 @@ test_files:
|
|
387
391
|
- spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb
|
388
392
|
- spec/unit/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb
|
389
393
|
- spec/unit/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb
|
390
|
-
- spec/unit/shoulda/matchers/active_model/validation_message_finder_spec.rb
|
391
394
|
- spec/unit/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb
|
392
395
|
- spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb
|
393
396
|
- spec/unit/shoulda/matchers/active_record/association_matchers/model_reflection_spec.rb
|
@@ -1,58 +0,0 @@
|
|
1
|
-
module Shoulda
|
2
|
-
module Matchers
|
3
|
-
module ActiveModel
|
4
|
-
# @private
|
5
|
-
class ExceptionMessageFinder
|
6
|
-
def initialize(instance, attribute, context=nil)
|
7
|
-
@instance = instance
|
8
|
-
@attribute = attribute
|
9
|
-
@context = context
|
10
|
-
end
|
11
|
-
|
12
|
-
def allow_description(allowed_values)
|
13
|
-
"doesn't raise when #{@attribute} is set to #{allowed_values}"
|
14
|
-
end
|
15
|
-
|
16
|
-
def messages_description
|
17
|
-
if has_messages?
|
18
|
-
": #{messages.join.inspect}"
|
19
|
-
else
|
20
|
-
' no exception'
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def has_messages?
|
25
|
-
messages.any?
|
26
|
-
end
|
27
|
-
|
28
|
-
def messages
|
29
|
-
@messages ||= validate_and_rescue
|
30
|
-
end
|
31
|
-
|
32
|
-
def source_description
|
33
|
-
'exception'
|
34
|
-
end
|
35
|
-
|
36
|
-
def expected_message_from(attribute_message)
|
37
|
-
"#{human_attribute_name} #{attribute_message}"
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
|
42
|
-
def validate_and_rescue
|
43
|
-
@instance.valid?(@context)
|
44
|
-
[]
|
45
|
-
rescue ::ActiveModel::StrictValidationFailed => exception
|
46
|
-
[exception.message]
|
47
|
-
end
|
48
|
-
|
49
|
-
def human_attribute_name
|
50
|
-
@instance.class.human_attribute_name(@attribute)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
|
@@ -1,69 +0,0 @@
|
|
1
|
-
module Shoulda
|
2
|
-
module Matchers
|
3
|
-
module ActiveModel
|
4
|
-
# @private
|
5
|
-
class ValidationMessageFinder
|
6
|
-
include Helpers
|
7
|
-
|
8
|
-
def initialize(instance, attribute, context=nil)
|
9
|
-
@instance = instance
|
10
|
-
@attribute = attribute
|
11
|
-
@context = context
|
12
|
-
end
|
13
|
-
|
14
|
-
def allow_description(allowed_values)
|
15
|
-
"allow #{@attribute} to be set to #{allowed_values}"
|
16
|
-
end
|
17
|
-
|
18
|
-
def expected_message_from(attribute_message)
|
19
|
-
attribute_message
|
20
|
-
end
|
21
|
-
|
22
|
-
def has_messages?
|
23
|
-
errors.present?
|
24
|
-
end
|
25
|
-
|
26
|
-
def source_description
|
27
|
-
'errors'
|
28
|
-
end
|
29
|
-
|
30
|
-
def messages_description
|
31
|
-
if errors.empty?
|
32
|
-
' no errors'
|
33
|
-
else
|
34
|
-
" errors:\n#{pretty_error_messages(validated_instance)}"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def messages
|
39
|
-
Array(messages_for_attribute)
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def messages_for_attribute
|
45
|
-
if errors.respond_to?(:[])
|
46
|
-
errors[@attribute]
|
47
|
-
else
|
48
|
-
errors.on(@attribute)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def errors
|
53
|
-
validated_instance.errors
|
54
|
-
end
|
55
|
-
|
56
|
-
def validated_instance
|
57
|
-
@validated_instance ||= validate_instance
|
58
|
-
end
|
59
|
-
|
60
|
-
def validate_instance
|
61
|
-
@instance.valid?(*@context)
|
62
|
-
@instance
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
@@ -1,111 +0,0 @@
|
|
1
|
-
require 'unit_spec_helper'
|
2
|
-
|
3
|
-
describe Shoulda::Matchers::ActiveModel::ExceptionMessageFinder, type: :model do
|
4
|
-
if active_model_3_2?
|
5
|
-
context '#allow_description' do
|
6
|
-
it 'describes its attribute' do
|
7
|
-
finder = build_finder(attribute: :attr)
|
8
|
-
|
9
|
-
description = finder.allow_description('allowed values')
|
10
|
-
|
11
|
-
expect(description).to eq %q(doesn't raise when attr is set to allowed values)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context '#expected_message_from' do
|
16
|
-
it 'returns the message with the attribute name prefixed' do
|
17
|
-
finder = build_finder(attribute: :attr)
|
18
|
-
|
19
|
-
message = finder.expected_message_from('some message')
|
20
|
-
|
21
|
-
expect(message).to eq 'Attr some message'
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context '#has_messages?' do
|
26
|
-
it 'has messages when some validations fail' do
|
27
|
-
finder = build_finder(format: /abc/, value: 'xyz')
|
28
|
-
|
29
|
-
result = finder.has_messages?
|
30
|
-
|
31
|
-
expect(result).to eq true
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'has no messages when all validations pass' do
|
35
|
-
finder = build_finder(format: /abc/, value: 'abc')
|
36
|
-
|
37
|
-
result = finder.has_messages?
|
38
|
-
|
39
|
-
expect(result).to eq false
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context '#messages' do
|
44
|
-
it 'returns errors for the given attribute' do
|
45
|
-
finder = build_finder(
|
46
|
-
attribute: :attr,
|
47
|
-
format: /abc/,
|
48
|
-
value: 'xyz'
|
49
|
-
)
|
50
|
-
|
51
|
-
messages = finder.messages
|
52
|
-
|
53
|
-
expect(messages).to eq ['Attr is invalid']
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context '#messages_description' do
|
58
|
-
it 'describes errors for the given attribute' do
|
59
|
-
finder = build_finder(
|
60
|
-
attribute: :attr,
|
61
|
-
format: /abc/,
|
62
|
-
value: 'xyz'
|
63
|
-
)
|
64
|
-
|
65
|
-
description = finder.messages_description
|
66
|
-
|
67
|
-
expect(description).to eq ': "Attr is invalid"'
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'describes errors when there are none' do
|
71
|
-
finder = build_finder(format: /abc/, value: 'abc')
|
72
|
-
|
73
|
-
description = finder.messages_description
|
74
|
-
|
75
|
-
expect(description).to eq ' no exception'
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
context '#source_description' do
|
80
|
-
it 'describes the source of its messages' do
|
81
|
-
finder = build_finder
|
82
|
-
|
83
|
-
description = finder.source_description
|
84
|
-
|
85
|
-
expect(description).to eq 'exception'
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def build_finder(arguments = {})
|
91
|
-
arguments[:attribute] ||= :attr
|
92
|
-
instance = build_instance_validating(
|
93
|
-
arguments[:attribute],
|
94
|
-
arguments[:format] || /abc/,
|
95
|
-
arguments[:value] || 'abc'
|
96
|
-
)
|
97
|
-
Shoulda::Matchers::ActiveModel::ExceptionMessageFinder.new(
|
98
|
-
instance,
|
99
|
-
arguments[:attribute]
|
100
|
-
)
|
101
|
-
end
|
102
|
-
|
103
|
-
def build_instance_validating(attribute, format, value)
|
104
|
-
model_class = define_model(:example, attribute => :string) do
|
105
|
-
attr_accessible attribute
|
106
|
-
validates_format_of attribute, with: format, strict: true
|
107
|
-
end
|
108
|
-
|
109
|
-
model_class.new(attribute => value)
|
110
|
-
end
|
111
|
-
end
|