shoulda-matchers 2.5.0 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -7
- data/.travis.yml +4 -0
- data/Appraisals +8 -0
- data/CONTRIBUTING.md +1 -1
- data/Gemfile +1 -1
- data/Gemfile.lock +77 -66
- data/MIT-LICENSE +1 -1
- data/NEWS.md +63 -1
- data/README.md +189 -33
- data/Rakefile +6 -5
- data/features/rails_integration.feature +1 -1
- data/features/step_definitions/rails_steps.rb +7 -6
- data/gemfiles/3.0.gemfile +2 -2
- data/gemfiles/3.0.gemfile.lock +14 -5
- data/gemfiles/3.1.gemfile +2 -2
- data/gemfiles/3.1.gemfile.lock +14 -5
- data/gemfiles/3.2.gemfile +2 -2
- data/gemfiles/3.2.gemfile.lock +16 -7
- data/gemfiles/4.0.0.gemfile +2 -2
- data/gemfiles/4.0.0.gemfile.lock +15 -6
- data/gemfiles/4.0.1.gemfile +2 -2
- data/gemfiles/4.0.1.gemfile.lock +15 -6
- data/gemfiles/4.1.gemfile +19 -0
- data/gemfiles/4.1.gemfile.lock +176 -0
- data/lib/shoulda/matchers.rb +17 -1
- data/lib/shoulda/matchers/action_controller.rb +4 -2
- data/lib/shoulda/matchers/action_controller/callback_matcher.rb +100 -0
- data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +1 -1
- data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +4 -4
- data/lib/shoulda/matchers/action_controller/rescue_from_matcher.rb +1 -1
- data/lib/shoulda/matchers/action_controller/route_matcher.rb +12 -12
- data/lib/shoulda/matchers/action_controller/route_params.rb +1 -1
- data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +2 -1
- data/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +167 -0
- data/lib/shoulda/matchers/active_model.rb +4 -2
- data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +23 -5
- data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +0 -4
- data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +66 -14
- data/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +8 -8
- data/lib/shoulda/matchers/active_model/errors.rb +40 -0
- data/lib/shoulda/matchers/active_model/helpers.rb +6 -6
- data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +33 -14
- data/lib/shoulda/matchers/active_model/numericality_matchers/even_number_matcher.rb +26 -0
- data/lib/shoulda/matchers/active_model/numericality_matchers/{odd_even_number_matcher.rb → numeric_type_matcher.rb} +9 -20
- data/lib/shoulda/matchers/active_model/numericality_matchers/odd_number_matcher.rb +26 -0
- data/lib/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher.rb +5 -21
- data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +1 -1
- data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +71 -22
- data/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +6 -1
- data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +25 -6
- data/lib/shoulda/matchers/active_record.rb +1 -0
- data/lib/shoulda/matchers/active_record/association_matcher.rb +67 -13
- data/lib/shoulda/matchers/active_record/association_matchers/inverse_of_matcher.rb +40 -0
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +24 -1
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb +1 -1
- data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +1 -1
- data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +1 -1
- data/lib/shoulda/matchers/assertion_error.rb +7 -2
- data/lib/shoulda/matchers/error.rb +24 -0
- data/lib/shoulda/matchers/independent.rb +10 -0
- data/lib/shoulda/matchers/independent/delegate_matcher.rb +157 -0
- data/lib/shoulda/matchers/independent/delegate_matcher/stubbed_target.rb +34 -0
- data/lib/shoulda/matchers/integrations/nunit_test_case_detection.rb +36 -0
- data/lib/shoulda/matchers/integrations/rspec.rb +13 -14
- data/lib/shoulda/matchers/integrations/test_unit.rb +11 -9
- data/lib/shoulda/matchers/version.rb +1 -1
- data/lib/shoulda/matchers/warn.rb +7 -0
- data/shoulda-matchers.gemspec +2 -1
- data/spec/shoulda/matchers/action_controller/callback_matcher_spec.rb +79 -0
- data/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +3 -3
- data/spec/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +11 -11
- data/spec/shoulda/matchers/action_controller/render_template_matcher_spec.rb +21 -21
- data/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +10 -10
- data/spec/shoulda/matchers/action_controller/rescue_from_matcher_spec.rb +45 -18
- data/spec/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +8 -8
- data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +19 -19
- data/spec/shoulda/matchers/action_controller/route_params_spec.rb +6 -6
- data/spec/shoulda/matchers/action_controller/set_session_matcher_spec.rb +11 -11
- data/spec/shoulda/matchers/action_controller/set_the_flash_matcher_spec.rb +44 -44
- data/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +205 -0
- data/spec/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +24 -24
- data/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +37 -37
- data/spec/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +17 -21
- data/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb +24 -24
- data/spec/shoulda/matchers/active_model/ensure_inclusion_of_matcher_spec.rb +173 -67
- data/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb +40 -40
- data/spec/shoulda/matchers/active_model/exception_message_finder_spec.rb +20 -20
- data/spec/shoulda/matchers/active_model/helpers_spec.rb +27 -25
- data/spec/shoulda/matchers/active_model/numericality_matchers/comparison_matcher_spec.rb +126 -13
- data/spec/shoulda/matchers/active_model/numericality_matchers/even_number_matcher_spec.rb +59 -0
- data/spec/shoulda/matchers/active_model/numericality_matchers/odd_number_matcher_spec.rb +59 -0
- data/spec/shoulda/matchers/active_model/numericality_matchers/only_integer_matcher_spec.rb +27 -26
- data/spec/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb +15 -15
- data/spec/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +8 -8
- data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +9 -9
- data/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +229 -44
- data/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +44 -25
- data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +110 -62
- data/spec/shoulda/matchers/active_model/validation_message_finder_spec.rb +19 -19
- data/spec/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +30 -30
- data/spec/shoulda/matchers/active_record/association_matcher_spec.rb +378 -192
- data/spec/shoulda/matchers/active_record/association_matchers/model_reflection_spec.rb +4 -0
- data/spec/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +33 -33
- data/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +21 -17
- data/spec/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +8 -8
- data/spec/shoulda/matchers/active_record/serialize_matcher_spec.rb +14 -14
- data/spec/shoulda/matchers/independent/delegate_matcher/stubbed_target_spec.rb +43 -0
- data/spec/shoulda/matchers/independent/delegate_matcher_spec.rb +184 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/activemodel_helpers.rb +2 -2
- data/spec/support/capture_helpers.rb +19 -0
- data/spec/support/controller_builder.rb +22 -3
- data/spec/support/fail_with_message_including_matcher.rb +33 -0
- data/spec/support/model_builder.rb +1 -1
- data/spec/support/shared_examples/numerical_submatcher.rb +19 -0
- data/spec/support/shared_examples/numerical_type_submatcher.rb +17 -0
- data/spec/support/test_application.rb +23 -0
- metadata +90 -22
- checksums.yaml +0 -7
- data/spec/shoulda/matchers/active_model/numericality_matchers/odd_even_number_matcher_spec.rb +0 -97
- data/spec/support/shared_examples/numerical_submatcher_spec.rb +0 -23
@@ -5,70 +5,70 @@ describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do
|
|
5
5
|
it 'describes itself with multiple values' do
|
6
6
|
matcher = allow_value('foo', 'bar').for(:baz)
|
7
7
|
|
8
|
-
matcher.description.
|
8
|
+
expect(matcher.description).to eq 'allow baz to be set to any of ["foo", "bar"]'
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'describes itself with a single value' do
|
12
12
|
matcher = allow_value('foo').for(:baz)
|
13
13
|
|
14
|
-
matcher.description.
|
14
|
+
expect(matcher.description).to eq 'allow baz to be set to "foo"'
|
15
15
|
end
|
16
16
|
|
17
17
|
if active_model_3_2?
|
18
18
|
it 'describes itself with a strict validation' do
|
19
19
|
strict_matcher = allow_value('xyz').for(:attr).strict
|
20
20
|
|
21
|
-
strict_matcher.description.
|
22
|
-
|
21
|
+
expect(strict_matcher.description).
|
22
|
+
to eq %q(doesn't raise when attr is set to "xyz")
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
context 'an attribute with a validation' do
|
28
28
|
it 'allows a good value' do
|
29
|
-
validating_format(:
|
29
|
+
expect(validating_format(with: /abc/)).to allow_value('abcde').for(:attr)
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'rejects a bad value' do
|
33
|
-
validating_format(:
|
33
|
+
expect(validating_format(with: /abc/)).not_to allow_value('xyz').for(:attr)
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'allows several good values' do
|
37
|
-
validating_format(:
|
38
|
-
|
37
|
+
expect(validating_format(with: /abc/)).
|
38
|
+
to allow_value('abcde', 'deabc').for(:attr)
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'rejects several bad values' do
|
42
|
-
validating_format(:
|
43
|
-
|
42
|
+
expect(validating_format(with: /abc/)).
|
43
|
+
not_to allow_value('xyz', 'zyx', nil, []).for(:attr)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
context 'an attribute with a validation and a custom message' do
|
48
48
|
it 'allows a good value' do
|
49
|
-
validating_format(:
|
50
|
-
|
49
|
+
expect(validating_format(with: /abc/, message: 'bad value')).
|
50
|
+
to allow_value('abcde').for(:attr).with_message(/bad/)
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'rejects a bad value' do
|
54
|
-
validating_format(:
|
55
|
-
|
54
|
+
expect(validating_format(with: /abc/, message: 'bad value')).
|
55
|
+
not_to allow_value('xyz').for(:attr).with_message(/bad/)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
context 'an attribute where the message occurs on another attribute' do
|
60
60
|
it 'allows a good value' do
|
61
|
-
record_with_custom_validation.
|
62
|
-
allow_value('good value').for(:attr).with_message(/some message/, :
|
61
|
+
expect(record_with_custom_validation).to \
|
62
|
+
allow_value('good value').for(:attr).with_message(/some message/, against: :attr2)
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'rejects a bad value' do
|
66
|
-
record_with_custom_validation.
|
67
|
-
allow_value('bad value').for(:attr).with_message(/some message/, :
|
66
|
+
expect(record_with_custom_validation).not_to \
|
67
|
+
allow_value('bad value').for(:attr).with_message(/some message/, against: :attr2)
|
68
68
|
end
|
69
69
|
|
70
70
|
def record_with_custom_validation
|
71
|
-
define_model :example, :
|
71
|
+
define_model :example, attr: :string, attr2: :string do
|
72
72
|
validate :custom_validation
|
73
73
|
|
74
74
|
def custom_validation
|
@@ -83,54 +83,54 @@ describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do
|
|
83
83
|
context "an attribute with a context-dependent validation" do
|
84
84
|
context "without the validation context" do
|
85
85
|
it "allows a bad value" do
|
86
|
-
validating_format(:
|
86
|
+
expect(validating_format(with: /abc/, on: :customisable)).to allow_value("xyz").for(:attr)
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
90
|
context "with the validation context" do
|
91
91
|
it "allows a good value" do
|
92
|
-
validating_format(:
|
92
|
+
expect(validating_format(with: /abc/, on: :customisable)).to allow_value("abcde").for(:attr).on(:customisable)
|
93
93
|
end
|
94
94
|
|
95
95
|
it "rejects a bad value" do
|
96
|
-
validating_format(:
|
96
|
+
expect(validating_format(with: /abc/, on: :customisable)).not_to allow_value("xyz").for(:attr).on(:customisable)
|
97
97
|
end
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
101
|
context 'an attribute with several validations' do
|
102
102
|
let(:model) do
|
103
|
-
define_model :example, :
|
103
|
+
define_model :example, attr: :string do
|
104
104
|
validates_presence_of :attr
|
105
|
-
validates_length_of :attr, :
|
106
|
-
validates_numericality_of :attr, :
|
107
|
-
:
|
105
|
+
validates_length_of :attr, within: 1..5
|
106
|
+
validates_numericality_of :attr, greater_than_or_equal_to: 1,
|
107
|
+
less_than_or_equal_to: 50000
|
108
108
|
end.new
|
109
109
|
end
|
110
110
|
bad_values = [nil, '', 'abc', '0', '50001', '123456', []]
|
111
111
|
|
112
112
|
it 'allows a good value' do
|
113
|
-
model.
|
113
|
+
expect(model).to allow_value('12345').for(:attr)
|
114
114
|
end
|
115
115
|
|
116
116
|
bad_values.each do |bad_value|
|
117
117
|
it "rejects a bad value (#{bad_value.inspect})" do
|
118
|
-
model.
|
118
|
+
expect(model).not_to allow_value(bad_value).for(:attr)
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|
122
122
|
it "rejects several bad values (#{bad_values.map(&:inspect).join(', ')})" do
|
123
|
-
model.
|
123
|
+
expect(model).not_to allow_value(*bad_values).for(:attr)
|
124
124
|
end
|
125
125
|
|
126
126
|
it "rejects a mix of both good and bad values" do
|
127
|
-
model.
|
127
|
+
expect(model).not_to allow_value('12345', *bad_values).for(:attr)
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
131
131
|
context 'with a single value' do
|
132
132
|
it 'allows you to call description before calling matches?' do
|
133
|
-
model = define_model(:example, :
|
133
|
+
model = define_model(:example, attr: :string).new
|
134
134
|
matcher = described_class.new('foo').for(:attr)
|
135
135
|
matcher.description
|
136
136
|
|
@@ -148,21 +148,21 @@ describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do
|
|
148
148
|
if active_model_3_2?
|
149
149
|
context 'an attribute with a strict format validation' do
|
150
150
|
it 'strictly rejects a bad value' do
|
151
|
-
validating_format(:
|
152
|
-
|
151
|
+
expect(validating_format(with: /abc/, strict: true)).
|
152
|
+
not_to allow_value('xyz').for(:attr).strict
|
153
153
|
end
|
154
154
|
|
155
155
|
it 'strictly allows a bad value with a different message' do
|
156
|
-
validating_format(:
|
157
|
-
|
156
|
+
expect(validating_format(with: /abc/, strict: true)).
|
157
|
+
to allow_value('xyz').for(:attr).with_message(/abc/).strict
|
158
158
|
end
|
159
159
|
|
160
160
|
it 'provides a useful negative failure message' do
|
161
161
|
matcher = allow_value('xyz').for(:attr).strict.with_message(/abc/)
|
162
162
|
|
163
|
-
matcher.matches?(validating_format(:
|
163
|
+
matcher.matches?(validating_format(with: /abc/, strict: true))
|
164
164
|
|
165
|
-
matcher.failure_message_when_negated.
|
165
|
+
expect(matcher.failure_message_when_negated).to eq 'Expected exception to include /abc/ ' +
|
166
166
|
'when attr is set to "xyz", got Attr is invalid'
|
167
167
|
end
|
168
168
|
end
|
@@ -1,73 +1,69 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Shoulda::Matchers::ActiveModel::DisallowValueMatcher do
|
4
|
-
it 'does not allow any types' do
|
5
|
-
matcher('abcde').allowed_types.should eq ''
|
6
|
-
end
|
7
|
-
|
8
4
|
context 'an attribute with a format validation' do
|
9
5
|
it 'does not match if the value is allowed' do
|
10
|
-
validating_format(:
|
6
|
+
expect(validating_format(with: /abc/)).not_to matcher('abcde').for(:attr)
|
11
7
|
end
|
12
8
|
|
13
9
|
it 'matches if the value is not allowed' do
|
14
|
-
validating_format(:
|
10
|
+
expect(validating_format(with: /abc/)).to matcher('xyz').for(:attr)
|
15
11
|
end
|
16
12
|
end
|
17
13
|
|
18
14
|
context "an attribute with a context-dependent validation" do
|
19
15
|
context "without the validation context" do
|
20
16
|
it "does not match" do
|
21
|
-
validating_format(:
|
17
|
+
expect(validating_format(with: /abc/, on: :customisable)).not_to matcher("xyz").for(:attr)
|
22
18
|
end
|
23
19
|
end
|
24
20
|
|
25
21
|
context "with the validation context" do
|
26
22
|
it "disallows a bad value" do
|
27
|
-
validating_format(:
|
23
|
+
expect(validating_format(with: /abc/, on: :customisable)).to matcher("xyz").for(:attr).on(:customisable)
|
28
24
|
end
|
29
25
|
|
30
26
|
it "does not match a good value" do
|
31
|
-
validating_format(:
|
27
|
+
expect(validating_format(with: /abc/, on: :customisable)).not_to matcher("abcde").for(:attr).on(:customisable)
|
32
28
|
end
|
33
29
|
end
|
34
30
|
end
|
35
31
|
|
36
32
|
context 'an attribute with a format validation and a custom message' do
|
37
33
|
it 'does not match if the value and message are both correct' do
|
38
|
-
validating_format(:
|
39
|
-
|
34
|
+
expect(validating_format(with: /abc/, message: 'good message')).
|
35
|
+
not_to matcher('abcde').for(:attr).with_message('good message')
|
40
36
|
end
|
41
37
|
|
42
38
|
it "delegates its failure message to its allow matcher's negative failure message" do
|
43
|
-
allow_matcher = stub_everything(:
|
39
|
+
allow_matcher = stub_everything(failure_message_when_negated: 'allow matcher failure')
|
44
40
|
Shoulda::Matchers::ActiveModel::AllowValueMatcher.stubs(:new).returns(allow_matcher)
|
45
41
|
|
46
42
|
matcher = matcher('abcde').for(:attr).with_message('good message')
|
47
|
-
matcher.matches?(validating_format(:
|
43
|
+
matcher.matches?(validating_format(with: /abc/, message: 'good message'))
|
48
44
|
|
49
|
-
matcher.failure_message.
|
45
|
+
expect(matcher.failure_message).to eq 'allow matcher failure'
|
50
46
|
end
|
51
47
|
|
52
48
|
it 'matches if the message is correct but the value is not' do
|
53
|
-
validating_format(:
|
54
|
-
|
49
|
+
expect(validating_format(with: /abc/, message: 'good message')).
|
50
|
+
to matcher('xyz').for(:attr).with_message('good message')
|
55
51
|
end
|
56
52
|
end
|
57
53
|
|
58
54
|
context 'an attribute where the message occurs on another attribute' do
|
59
55
|
it 'matches if the message is correct but the value is not' do
|
60
|
-
record_with_custom_validation.
|
61
|
-
matcher('bad value').for(:attr).with_message(/some message/, :
|
56
|
+
expect(record_with_custom_validation).to \
|
57
|
+
matcher('bad value').for(:attr).with_message(/some message/, against: :attr2)
|
62
58
|
end
|
63
59
|
|
64
60
|
it 'does not match if the value and message are both correct' do
|
65
|
-
record_with_custom_validation.
|
66
|
-
matcher('good value').for(:attr).with_message(/some message/, :
|
61
|
+
expect(record_with_custom_validation).not_to \
|
62
|
+
matcher('good value').for(:attr).with_message(/some message/, against: :attr2)
|
67
63
|
end
|
68
64
|
|
69
65
|
def record_with_custom_validation
|
70
|
-
define_model :example, :
|
66
|
+
define_model :example, attr: :string, attr2: :string do
|
71
67
|
validate :custom_validation
|
72
68
|
|
73
69
|
def custom_validation
|
@@ -3,37 +3,37 @@ require 'spec_helper'
|
|
3
3
|
describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
|
4
4
|
context 'an attribute which must be excluded from a range' do
|
5
5
|
it 'accepts ensuring the correct range' do
|
6
|
-
validating_exclusion(:
|
7
|
-
|
6
|
+
expect(validating_exclusion(in: 2..5)).
|
7
|
+
to ensure_exclusion_of(:attr).in_range(2..5)
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'rejects ensuring excluded value' do
|
11
|
-
validating_exclusion(:
|
12
|
-
|
11
|
+
expect(validating_exclusion(in: 2..5)).
|
12
|
+
not_to ensure_exclusion_of(:attr).in_range(2..6)
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'does not override the default message with a blank' do
|
16
|
-
validating_exclusion(:
|
17
|
-
|
16
|
+
expect(validating_exclusion(in: 2..5)).
|
17
|
+
to ensure_exclusion_of(:attr).in_range(2..5).with_message(nil)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
context 'an attribute which must be excluded from a range with excluded end' do
|
22
22
|
it 'accepts ensuring the correct range' do
|
23
|
-
validating_exclusion(:
|
24
|
-
|
23
|
+
expect(validating_exclusion(in: 2...5)).
|
24
|
+
to ensure_exclusion_of(:attr).in_range(2...5)
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'rejects ensuring excluded value' do
|
28
|
-
validating_exclusion(:
|
29
|
-
|
28
|
+
expect(validating_exclusion(in: 2...5)).
|
29
|
+
not_to ensure_exclusion_of(:attr).in_range(2...4)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
context 'an attribute with a custom validation message' do
|
34
34
|
it 'accepts ensuring the correct range' do
|
35
|
-
validating_exclusion(:
|
36
|
-
|
35
|
+
expect(validating_exclusion(in: 2..4, message: 'not good')).
|
36
|
+
to ensure_exclusion_of(:attr).in_range(2..4).with_message(/not good/)
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -45,7 +45,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
model.
|
48
|
+
expect(model).to ensure_exclusion_of(:attr).in_range(2..5).
|
49
49
|
with_message(/should be out of this range/)
|
50
50
|
|
51
51
|
model = custom_validation do
|
@@ -54,41 +54,41 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
model.
|
57
|
+
expect(model).to ensure_exclusion_of(:attr).in_range(2...5).
|
58
58
|
with_message(/should be out of this range/)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
context 'an attribute which must be excluded from an array' do
|
63
63
|
it 'accepts with correct array' do
|
64
|
-
validating_exclusion(:
|
65
|
-
|
64
|
+
expect(validating_exclusion(in: %w(one two))).
|
65
|
+
to ensure_exclusion_of(:attr).in_array(%w(one two))
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'rejects when only part of array matches' do
|
69
|
-
validating_exclusion(:
|
70
|
-
|
69
|
+
expect(validating_exclusion(in: %w(one two))).
|
70
|
+
not_to ensure_exclusion_of(:attr).in_array(%w(one wrong_value))
|
71
71
|
end
|
72
72
|
|
73
73
|
it 'rejects when array does not match at all' do
|
74
|
-
validating_exclusion(:
|
75
|
-
|
74
|
+
expect(validating_exclusion(in: %w(one two))).
|
75
|
+
not_to ensure_exclusion_of(:attr).in_array(%w(cat dog))
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'has correct description' do
|
79
|
-
ensure_exclusion_of(:attr).in_array([true, 'dog']).description.
|
80
|
-
|
79
|
+
expect(ensure_exclusion_of(:attr).in_array([true, 'dog']).description).
|
80
|
+
to eq 'ensure exclusion of attr in [true, "dog"]'
|
81
81
|
end
|
82
82
|
|
83
83
|
def validating_exclusion(options)
|
84
|
-
define_model(:example, :
|
84
|
+
define_model(:example, attr: :string) do
|
85
85
|
validates_exclusion_of :attr, options
|
86
86
|
end.new
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
90
|
def validating_exclusion(options)
|
91
|
-
define_model(:example, :
|
91
|
+
define_model(:example, attr: :integer) do
|
92
92
|
validates_exclusion_of :attr, options
|
93
93
|
end.new
|
94
94
|
end
|
@@ -3,95 +3,95 @@ require 'spec_helper'
|
|
3
3
|
describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
|
4
4
|
context 'with no validations' do
|
5
5
|
it 'rejects an array which does not have a validator defined' do
|
6
|
-
define_model(:example, :
|
7
|
-
|
6
|
+
expect(define_model(:example, attr: :string).new).
|
7
|
+
not_to ensure_inclusion_of(:attr).in_array(%w(Yes No))
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
context 'with an integer column' do
|
12
12
|
it 'can verify a zero in the array' do
|
13
|
-
model = define_model(:example, :
|
14
|
-
validates_inclusion_of :attr, :
|
13
|
+
model = define_model(:example, attr: :integer) do
|
14
|
+
validates_inclusion_of :attr, in: [0, 1, 2]
|
15
15
|
end.new
|
16
16
|
|
17
|
-
model.
|
17
|
+
expect(model).to ensure_inclusion_of(:attr).in_array([0,1,2])
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
context 'with an decimal column' do
|
22
22
|
it 'can verify decimal values' do
|
23
|
-
model = define_model(:example, :
|
24
|
-
validates_inclusion_of :attr, :
|
23
|
+
model = define_model(:example, attr: :decimal) do
|
24
|
+
validates_inclusion_of :attr, in: [0.0, 0.1]
|
25
25
|
end.new
|
26
26
|
|
27
|
-
model.
|
27
|
+
expect(model).to ensure_inclusion_of(:attr).in_array([0.0, 0.1])
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
context 'with true/false values' do
|
32
32
|
it 'can verify outside values to ensure the negative case' do
|
33
|
-
define_model(:example, :
|
34
|
-
|
33
|
+
expect(define_model(:example, attr: :string).new).
|
34
|
+
not_to ensure_inclusion_of(:attr).in_array([true, false])
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
38
|
context 'where we cannot determine a value outside the array' do
|
39
39
|
it 'raises a custom exception' do
|
40
|
-
model = define_model(:example, :
|
40
|
+
model = define_model(:example, attr: :string).new
|
41
41
|
|
42
42
|
arbitrary_string = described_class::ARBITRARY_OUTSIDE_STRING
|
43
|
-
expect { model.
|
43
|
+
expect { expect(model).to ensure_inclusion_of(:attr).in_array([arbitrary_string]) }.to raise_error Shoulda::Matchers::ActiveModel::CouldNotDetermineValueOutsideOfArray
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
context 'an attribute which must be included in a range' do
|
48
48
|
it 'accepts ensuring the correct range' do
|
49
|
-
validating_inclusion(:
|
50
|
-
|
49
|
+
expect(validating_inclusion(in: 2..5)).
|
50
|
+
to ensure_inclusion_of(:attr).in_range(2..5)
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'rejects ensuring a lower minimum value' do
|
54
|
-
validating_inclusion(:
|
55
|
-
|
54
|
+
expect(validating_inclusion(in: 2..5)).
|
55
|
+
not_to ensure_inclusion_of(:attr).in_range(1..5)
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'rejects ensuring a higher minimum value' do
|
59
|
-
validating_inclusion(:
|
60
|
-
|
59
|
+
expect(validating_inclusion(in: 2..5)).
|
60
|
+
not_to ensure_inclusion_of(:attr).in_range(3..5)
|
61
61
|
end
|
62
62
|
|
63
63
|
it 'rejects ensuring a lower maximum value' do
|
64
|
-
validating_inclusion(:
|
65
|
-
|
64
|
+
expect(validating_inclusion(in: 2..5)).
|
65
|
+
not_to ensure_inclusion_of(:attr).in_range(2..4)
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'rejects ensuring a higher maximum value' do
|
69
|
-
validating_inclusion(:
|
70
|
-
|
69
|
+
expect(validating_inclusion(in: 2..5)).
|
70
|
+
not_to ensure_inclusion_of(:attr).in_range(2..6)
|
71
71
|
end
|
72
72
|
|
73
73
|
it 'does not override the default message with a blank' do
|
74
|
-
validating_inclusion(:
|
75
|
-
|
74
|
+
expect(validating_inclusion(in: 2..5)).
|
75
|
+
to ensure_inclusion_of(:attr).in_range(2..5).with_message(nil)
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
79
|
context 'an attribute which must be included in a range with excluded end' do
|
80
80
|
it 'accepts ensuring the correct range' do
|
81
|
-
validating_inclusion(:
|
82
|
-
|
81
|
+
expect(validating_inclusion(in: 2...5)).
|
82
|
+
to ensure_inclusion_of(:attr).in_range(2...5)
|
83
83
|
end
|
84
84
|
|
85
85
|
it 'rejects ensuring a lower maximum value' do
|
86
|
-
validating_inclusion(:
|
87
|
-
|
86
|
+
expect(validating_inclusion(in: 2...5)).
|
87
|
+
not_to ensure_inclusion_of(:attr).in_range(2...4)
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
91
|
context 'an attribute with a custom ranged value validation' do
|
92
92
|
it 'accepts ensuring the correct range' do
|
93
|
-
validating_inclusion(:
|
94
|
-
|
93
|
+
expect(validating_inclusion(in: 2..4, message: 'not good')).
|
94
|
+
to ensure_inclusion_of(:attr).in_range(2..4).with_message(/not good/)
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
@@ -105,7 +105,7 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
|
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
108
|
-
model.
|
108
|
+
expect(model).to ensure_inclusion_of(:attr).in_range(2..5).
|
109
109
|
with_low_message(/low/).with_high_message(/high/)
|
110
110
|
|
111
111
|
model = custom_validation do
|
@@ -116,99 +116,205 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
|
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
|
-
model.
|
119
|
+
expect(model).to ensure_inclusion_of(:attr).in_range(2...5).
|
120
120
|
with_low_message(/low/).with_high_message(/high/)
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
124
124
|
context 'an attribute which must be included in an array' do
|
125
125
|
it 'accepts with correct array' do
|
126
|
-
validating_inclusion(:
|
127
|
-
|
126
|
+
expect(validating_inclusion(in: %w(one two))).
|
127
|
+
to ensure_inclusion_of(:attr).in_array(%w(one two))
|
128
128
|
end
|
129
129
|
|
130
130
|
it 'rejects when only part of array matches' do
|
131
|
-
validating_inclusion(:
|
132
|
-
|
131
|
+
expect(validating_inclusion(in: %w(one two))).
|
132
|
+
not_to ensure_inclusion_of(:attr).in_array(%w(one wrong_value))
|
133
133
|
end
|
134
134
|
|
135
135
|
it 'rejects when array does not match at all' do
|
136
|
-
validating_inclusion(:
|
137
|
-
|
136
|
+
expect(validating_inclusion(in: %w(one two))).
|
137
|
+
not_to ensure_inclusion_of(:attr).in_array(%w(cat dog))
|
138
138
|
end
|
139
139
|
|
140
140
|
it 'has correct description' do
|
141
|
-
ensure_inclusion_of(:attr).in_array([true, "dog"]).description.
|
142
|
-
|
141
|
+
expect(ensure_inclusion_of(:attr).in_array([true, "dog"]).description).
|
142
|
+
to eq 'ensure inclusion of attr in [true, "dog"]'
|
143
143
|
end
|
144
144
|
|
145
145
|
it 'rejects allow_blank' do
|
146
|
-
validating_inclusion(:
|
147
|
-
|
146
|
+
expect(validating_inclusion(in: %w(one two))).
|
147
|
+
not_to ensure_inclusion_of(:attr).in_array(%w(one two)).allow_blank(true)
|
148
148
|
end
|
149
149
|
|
150
150
|
it 'accepts allow_blank(false)' do
|
151
|
-
validating_inclusion(:
|
152
|
-
|
151
|
+
expect(validating_inclusion(in: %w(one two))).
|
152
|
+
to ensure_inclusion_of(:attr).in_array(%w(one two)).allow_blank(false)
|
153
153
|
end
|
154
154
|
|
155
155
|
it 'rejects allow_nil' do
|
156
|
-
validating_inclusion(:
|
157
|
-
|
156
|
+
expect(validating_inclusion(in: %w(one two))).
|
157
|
+
not_to ensure_inclusion_of(:attr).in_array(%w(one two)).allow_nil(true)
|
158
158
|
end
|
159
159
|
|
160
160
|
it 'accepts allow_nil(false)' do
|
161
|
-
validating_inclusion(:
|
162
|
-
|
161
|
+
expect(validating_inclusion(in: %w(one two))).
|
162
|
+
to ensure_inclusion_of(:attr).in_array(%w(one two)).allow_nil(false)
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
166
166
|
context 'with allowed blank and allowed nil' do
|
167
167
|
it 'accepts allow_blank' do
|
168
|
-
validating_inclusion(:
|
169
|
-
|
168
|
+
expect(validating_inclusion(in: %w(one two), allow_blank: true)).
|
169
|
+
to ensure_inclusion_of(:attr).in_array(%w(one two)).allow_blank
|
170
170
|
end
|
171
171
|
|
172
172
|
it 'rejects allow_blank(false)' do
|
173
|
-
validating_inclusion(:
|
174
|
-
|
173
|
+
expect(validating_inclusion(in: %w(one two), allow_blank: true)).
|
174
|
+
not_to ensure_inclusion_of(:attr).in_array(%w(one two)).allow_blank(false)
|
175
175
|
end
|
176
176
|
|
177
177
|
it 'accepts allow_nil' do
|
178
|
-
validating_inclusion(:
|
179
|
-
|
178
|
+
expect(validating_inclusion(in: %w(one two), allow_nil: true)).
|
179
|
+
to ensure_inclusion_of(:attr).in_array(%w(one two)).allow_nil
|
180
180
|
end
|
181
181
|
|
182
182
|
it 'rejects allow_nil' do
|
183
|
-
validating_inclusion(:
|
184
|
-
|
183
|
+
expect(validating_inclusion(in: %w(one two), allow_nil: true)).
|
184
|
+
not_to ensure_inclusion_of(:attr).in_array(%w(one two)).allow_nil(false)
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
188
188
|
context 'an attribute allowing some blank values but not others' do
|
189
189
|
it 'rejects allow_blank' do
|
190
|
-
validating_inclusion(:
|
191
|
-
|
190
|
+
expect(validating_inclusion(in: ['one', 'two', ''])).
|
191
|
+
not_to ensure_inclusion_of(:attr).in_array(['one', 'two', '']).allow_blank(true)
|
192
192
|
end
|
193
193
|
end
|
194
194
|
|
195
195
|
if active_model_3_2?
|
196
196
|
context 'a strict attribute which must be included in a range' do
|
197
197
|
it 'accepts ensuring the correct range' do
|
198
|
-
validating_inclusion(:
|
199
|
-
|
198
|
+
expect(validating_inclusion(in: 2..5, strict: true)).
|
199
|
+
to ensure_inclusion_of(:attr).in_range(2..5).strict
|
200
200
|
end
|
201
201
|
|
202
202
|
it 'rejects ensuring another range' do
|
203
|
-
validating_inclusion(:
|
204
|
-
|
203
|
+
expect(validating_inclusion(in: 2..5, strict: true)).
|
204
|
+
not_to ensure_inclusion_of(:attr).in_range(2..6).strict
|
205
205
|
end
|
206
206
|
end
|
207
207
|
end
|
208
|
-
end
|
209
208
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
209
|
+
context 'against a boolean attribute' do
|
210
|
+
context 'which is nullable' do
|
211
|
+
context 'when ensuring inclusion of true' do
|
212
|
+
it "doesn't raise an error" do
|
213
|
+
record = validating_inclusion_of_boolean_in(:attr, [true], null: true)
|
214
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([true])
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context 'when ensuring inclusion of false' do
|
219
|
+
it "doesn't raise an error" do
|
220
|
+
record = validating_inclusion_of_boolean_in(:attr, [false], null: true)
|
221
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([false])
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
context 'when ensuring inclusion of true and false' do
|
226
|
+
it "doesn't raise an error" do
|
227
|
+
record = validating_inclusion_of_boolean_in(:attr, [true, false], null: true)
|
228
|
+
capture(:stderr) do
|
229
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([true, false])
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'prints a warning' do
|
234
|
+
record = validating_inclusion_of_boolean_in(:attr, [true, false], null: true)
|
235
|
+
stderr = capture(:stderr) do
|
236
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([true, false])
|
237
|
+
end
|
238
|
+
expect(stderr.gsub(/\n+/, ' ')).
|
239
|
+
to include('You are using `ensure_inclusion_of` to assert that a boolean column allows boolean values and disallows non-boolean ones')
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
context 'when ensuring inclusion of nil' do
|
244
|
+
it "doesn't raise an error" do
|
245
|
+
record = validating_inclusion_of_boolean_in(:attr, [nil], null: true)
|
246
|
+
capture(:stderr) do
|
247
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([nil])
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
it 'prints a warning' do
|
252
|
+
record = validating_inclusion_of_boolean_in(:attr, [nil], null: true)
|
253
|
+
stderr = capture(:stderr) do
|
254
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([nil])
|
255
|
+
end
|
256
|
+
expect(stderr.gsub(/\n+/, ' ')).
|
257
|
+
to include('You are using `ensure_inclusion_of` to assert that a boolean column allows nil')
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
context 'which is non-nullable' do
|
263
|
+
context 'when ensuring inclusion of true' do
|
264
|
+
it "doesn't raise an error" do
|
265
|
+
record = validating_inclusion_of_boolean_in(:attr, [true], null: false)
|
266
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([true])
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
context 'when ensuring inclusion of false' do
|
271
|
+
it "doesn't raise an error" do
|
272
|
+
record = validating_inclusion_of_boolean_in(:attr, [false], null: false)
|
273
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([false])
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
context 'when ensuring inclusion of true and false' do
|
278
|
+
it "doesn't raise an error" do
|
279
|
+
record = validating_inclusion_of_boolean_in(:attr, [true, false], null: false)
|
280
|
+
capture(:stderr) do
|
281
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([true, false])
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
it 'prints a warning' do
|
286
|
+
record = validating_inclusion_of_boolean_in(:attr, [true, false], null: false)
|
287
|
+
stderr = capture(:stderr) do
|
288
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([true, false])
|
289
|
+
end
|
290
|
+
expect(stderr.gsub(/\n+/, ' ')).
|
291
|
+
to include('You are using `ensure_inclusion_of` to assert that a boolean column allows boolean values and disallows non-boolean ones')
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
context 'when ensuring inclusion of nil' do
|
296
|
+
it 'raises a specific error' do
|
297
|
+
record = validating_inclusion_of_boolean_in(:attr, [nil], null: false)
|
298
|
+
error_class = Shoulda::Matchers::ActiveModel::NonNullableBooleanError
|
299
|
+
expect {
|
300
|
+
expect(record).to ensure_inclusion_of(:attr).in_array([nil])
|
301
|
+
}.to raise_error(error_class)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
def validating_inclusion(options)
|
308
|
+
define_model(:example, attr: :string) do
|
309
|
+
validates_inclusion_of :attr, options
|
310
|
+
end.new
|
311
|
+
end
|
312
|
+
|
313
|
+
def validating_inclusion_of_boolean_in(attribute, values, options = {})
|
314
|
+
null = options.fetch(:null, true)
|
315
|
+
column_options = { type: :boolean, options: { null: null } }
|
316
|
+
define_model(:example, attribute => column_options) do
|
317
|
+
validates_inclusion_of attribute, in: values
|
318
|
+
end.new
|
319
|
+
end
|
214
320
|
end
|