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
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::EvenNumberMatcher do
|
4
|
+
subject { described_class.new(:attr) }
|
5
|
+
|
6
|
+
it_behaves_like 'a numerical submatcher'
|
7
|
+
it_behaves_like 'a numerical type submatcher'
|
8
|
+
|
9
|
+
it 'allows even number' do
|
10
|
+
expect(subject.allowed_type).to eq 'even numbers'
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#diff_to_compare' do
|
14
|
+
it { expect(subject.diff_to_compare).to eq 2 }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when the model has an even validation' do
|
18
|
+
it 'matches' do
|
19
|
+
match = subject
|
20
|
+
expect(validating_even_number).to match
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when the model does not have an even validation' do
|
25
|
+
it 'does not match' do
|
26
|
+
match = subject
|
27
|
+
expect(not_validating_even_number).not_to match
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'fails with the ActiveRecord :even message' do
|
31
|
+
match = subject
|
32
|
+
expect {
|
33
|
+
expect(not_validating_even_number).to match
|
34
|
+
}.to fail_with_message_including('Expected errors to include "must be even"')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with custom validation message' do
|
39
|
+
it 'only accepts even number values for that attribute with that message' do
|
40
|
+
expect(validating_even_number(message: 'custom')).to subject.with_message(/custom/)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'fails even number values for that attribute with another message' do
|
44
|
+
expect(validating_even_number(message: 'custom')).not_to subject.with_message(/wrong/)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def validating_even_number(options = {})
|
50
|
+
define_model :example, attr: :string do
|
51
|
+
validates_numericality_of :attr, { even: true }.merge(options)
|
52
|
+
end.new
|
53
|
+
end
|
54
|
+
|
55
|
+
def not_validating_even_number
|
56
|
+
define_model(:example, attr: :string).new
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::OddNumberMatcher do
|
4
|
+
subject { described_class.new(:attr) }
|
5
|
+
|
6
|
+
it_behaves_like 'a numerical submatcher'
|
7
|
+
it_behaves_like 'a numerical type submatcher'
|
8
|
+
|
9
|
+
it 'allows odd number' do
|
10
|
+
expect(subject.allowed_type).to eq 'odd numbers'
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#diff_to_compare' do
|
14
|
+
it { expect(subject.diff_to_compare).to eq 2 }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when the model has an odd validation' do
|
18
|
+
it 'matches' do
|
19
|
+
match = subject
|
20
|
+
expect(validating_odd_number).to match
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when the model does not have an odd validation' do
|
25
|
+
it 'does not match' do
|
26
|
+
match = subject
|
27
|
+
expect(not_validating_odd_number).not_to match
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'fails with the ActiveRecord :odd message' do
|
31
|
+
match = subject
|
32
|
+
expect {
|
33
|
+
expect(not_validating_odd_number).to match
|
34
|
+
}.to fail_with_message_including('Expected errors to include "must be odd"')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with custom validation message' do
|
39
|
+
it 'only accepts odd number values for that attribute with that message' do
|
40
|
+
expect(validating_odd_number(message: 'custom')).to subject.with_message(/custom/)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'fails odd number values for that attribute with another message' do
|
44
|
+
expect(validating_odd_number(message: 'custom')).not_to subject.with_message(/wrong/)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def validating_odd_number(options = {})
|
50
|
+
define_model :example, attr: :string do
|
51
|
+
validates_numericality_of :attr, { odd: true }.merge(options)
|
52
|
+
end.new
|
53
|
+
end
|
54
|
+
|
55
|
+
def not_validating_odd_number
|
56
|
+
define_model(:example, attr: :string).new
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -1,56 +1,57 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Shoulda::Matchers::ActiveModel::NumericalityMatchers::OnlyIntegerMatcher do
|
4
|
-
|
5
|
-
|
4
|
+
subject { described_class.new(:attr) }
|
5
|
+
|
6
|
+
it_behaves_like 'a numerical submatcher'
|
7
|
+
it_behaves_like 'a numerical type submatcher'
|
8
|
+
|
9
|
+
it 'allows integer types' do
|
10
|
+
expect(subject.allowed_type).to eq 'integers'
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#diff_to_compare' do
|
14
|
+
it { expect(subject.diff_to_compare).to eq 1 }
|
6
15
|
end
|
7
16
|
|
8
17
|
context 'given an attribute that only allows integer values' do
|
9
18
|
it 'matches' do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
it 'allows integer types' do
|
14
|
-
new_matcher.allowed_types.should eq 'integer'
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'returns itself when given a message' do
|
18
|
-
matcher = new_matcher
|
19
|
-
matcher.with_message('some message').should eq matcher
|
19
|
+
match = subject
|
20
|
+
expect(validating_only_integer).to match
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
24
|
context 'given an attribute that only allows integer values with a custom validation message' do
|
24
25
|
it 'only accepts integer values for that attribute with that message' do
|
25
|
-
validating_only_integer(:
|
26
|
+
expect(validating_only_integer(message: 'custom')).to subject.with_message(/custom/)
|
26
27
|
end
|
27
28
|
|
28
29
|
it 'rejects integer values for that attribute with another message' do
|
29
|
-
validating_only_integer(:
|
30
|
+
expect(validating_only_integer(message: 'custom')).not_to subject.with_message(/wrong/)
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
34
|
context 'when the model does not have an only_integer validation' do
|
34
35
|
it 'does not match' do
|
35
|
-
|
36
|
+
match = subject
|
37
|
+
expect(not_validating_only_integer).not_to match
|
36
38
|
end
|
37
39
|
|
38
40
|
it 'fails with the ActiveRecord :not_an_integer message' do
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
matcher.failure_message.should include 'Expected errors to include "must be an integer"'
|
41
|
+
match = subject
|
42
|
+
expect {
|
43
|
+
expect(not_validating_only_integer).to match
|
44
|
+
}.to fail_with_message_including('Expected errors to include "must be an integer"')
|
44
45
|
end
|
45
46
|
end
|
46
47
|
|
47
|
-
def new_matcher
|
48
|
-
described_class.new(:attr)
|
49
|
-
end
|
50
|
-
|
51
48
|
def validating_only_integer(options = {})
|
52
|
-
define_model :example, :
|
53
|
-
validates_numericality_of :attr, { :
|
49
|
+
define_model :example, attr: :string do
|
50
|
+
validates_numericality_of :attr, { only_integer: true }.merge(options)
|
54
51
|
end.new
|
55
52
|
end
|
53
|
+
|
54
|
+
def not_validating_only_integer
|
55
|
+
define_model(:example, attr: :string).new
|
56
|
+
end
|
56
57
|
end
|
@@ -4,67 +4,67 @@ describe Shoulda::Matchers::ActiveModel::ValidateAbsenceOfMatcher do
|
|
4
4
|
if active_model_4_0?
|
5
5
|
context 'a model with an absence validation' do
|
6
6
|
it 'accepts' do
|
7
|
-
validating_absence_of(:attr).
|
7
|
+
expect(validating_absence_of(:attr)).to validate_absence_of(:attr)
|
8
8
|
end
|
9
9
|
|
10
10
|
it 'does not override the default message with a present' do
|
11
|
-
validating_absence_of(:attr).
|
11
|
+
expect(validating_absence_of(:attr)).to validate_absence_of(:attr).with_message(nil)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
context 'a model without an absence validation' do
|
16
16
|
it 'rejects' do
|
17
17
|
model = define_model(:example, attr: :string).new
|
18
|
-
model.
|
18
|
+
expect(model).not_to validate_absence_of(:attr)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
context 'an ActiveModel class with an absence validation' do
|
23
23
|
it 'accepts' do
|
24
|
-
active_model_validating_absence_of(:attr).
|
24
|
+
expect(active_model_validating_absence_of(:attr)).to validate_absence_of(:attr)
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'does not override the default message with a blank' do
|
28
|
-
active_model_validating_absence_of(:attr).
|
28
|
+
expect(active_model_validating_absence_of(:attr)).to validate_absence_of(:attr).with_message(nil)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
context 'an ActiveModel class without an absence validation' do
|
33
33
|
it 'rejects' do
|
34
|
-
active_model_with(:attr).
|
34
|
+
expect(active_model_with(:attr)).not_to validate_absence_of(:attr)
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'provides the correct failure message' do
|
38
38
|
message = %{Expected errors to include "must be blank" when attr is set to "an arbitrary value", got no errors}
|
39
39
|
|
40
|
-
expect { active_model_with(:attr).
|
40
|
+
expect { expect(active_model_with(:attr)).to validate_absence_of(:attr) }.to fail_with_message(message)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
44
|
context 'a has_many association with an absence validation' do
|
45
45
|
it 'requires the attribute to not be set' do
|
46
|
-
having_many(:children, absence: true).
|
46
|
+
expect(having_many(:children, absence: true)).to validate_absence_of(:children)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
context 'a has_many association without an absence validation' do
|
51
51
|
it 'does not require the attribute to not be set' do
|
52
|
-
having_many(:children, absence: false).
|
53
|
-
|
52
|
+
expect(having_many(:children, absence: false)).
|
53
|
+
not_to validate_absence_of(:children)
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
57
|
context 'an absent has_and_belongs_to_many association' do
|
58
58
|
it 'accepts' do
|
59
59
|
model = having_and_belonging_to_many(:children, absence: true)
|
60
|
-
model.
|
60
|
+
expect(model).to validate_absence_of(:children)
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
64
|
context 'a non-absent has_and_belongs_to_many association' do
|
65
65
|
it 'rejects' do
|
66
66
|
model = having_and_belonging_to_many(:children, absence: false)
|
67
|
-
model.
|
67
|
+
expect(model).not_to validate_absence_of(:children)
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -76,7 +76,7 @@ describe Shoulda::Matchers::ActiveModel::ValidateAbsenceOfMatcher do
|
|
76
76
|
"%{attribute} must be blank in a %{model}")
|
77
77
|
|
78
78
|
expect {
|
79
|
-
validating_absence_of(:attr).
|
79
|
+
expect(validating_absence_of(:attr)).to validate_absence_of(:attr)
|
80
80
|
}.to_not raise_exception
|
81
81
|
end
|
82
82
|
end
|
@@ -84,13 +84,13 @@ describe Shoulda::Matchers::ActiveModel::ValidateAbsenceOfMatcher do
|
|
84
84
|
context "an attribute with a context-dependent validation" do
|
85
85
|
context "without the validation context" do
|
86
86
|
it "does not match" do
|
87
|
-
validating_absence_of(:attr, on: :customisable).
|
87
|
+
expect(validating_absence_of(:attr, on: :customisable)).not_to validate_absence_of(:attr)
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
91
|
context "with the validation context" do
|
92
92
|
it "matches" do
|
93
|
-
validating_absence_of(:attr, on: :customisable).
|
93
|
+
expect(validating_absence_of(:attr, on: :customisable)).to validate_absence_of(:attr).on(:customisable)
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|
@@ -3,29 +3,29 @@ require 'spec_helper'
|
|
3
3
|
describe Shoulda::Matchers::ActiveModel::ValidateAcceptanceOfMatcher do
|
4
4
|
context 'a model with an acceptance validation' do
|
5
5
|
it 'accepts when the attributes match' do
|
6
|
-
validating_acceptance.
|
6
|
+
expect(validating_acceptance).to matcher
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'does not overwrite the default message with nil' do
|
10
|
-
validating_acceptance.
|
10
|
+
expect(validating_acceptance).to matcher.with_message(nil)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
context 'a model without an acceptance validation' do
|
15
15
|
it 'rejects' do
|
16
|
-
define_model(:example, :
|
16
|
+
expect(define_model(:example, attr: :string).new).not_to matcher
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
context 'an attribute which must be accepted with a custom message' do
|
21
21
|
it 'accepts when the message matches' do
|
22
|
-
validating_acceptance(:
|
23
|
-
|
22
|
+
expect(validating_acceptance(message: 'custom')).
|
23
|
+
to matcher.with_message(/custom/)
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'rejects when the message does not match' do
|
27
|
-
validating_acceptance(:
|
28
|
-
|
27
|
+
expect(validating_acceptance(message: 'custom')).
|
28
|
+
not_to matcher.with_message(/wrong/)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -34,7 +34,7 @@ describe Shoulda::Matchers::ActiveModel::ValidateAcceptanceOfMatcher do
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def validating_acceptance(options = {})
|
37
|
-
define_model(:example, :
|
37
|
+
define_model(:example, attr: :string) do
|
38
38
|
validates_acceptance_of :attr, options
|
39
39
|
end.new
|
40
40
|
end
|
@@ -3,35 +3,35 @@ require 'spec_helper'
|
|
3
3
|
describe Shoulda::Matchers::ActiveModel::ValidateConfirmationOfMatcher do
|
4
4
|
context '#description' do
|
5
5
|
it 'states that the confirmation must match its base attribute' do
|
6
|
-
matcher.description.
|
6
|
+
expect(matcher.description).to eq 'require attr_confirmation to match attr'
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
context 'a model with a confirmation validation' do
|
11
11
|
it 'accepts' do
|
12
|
-
validating_confirmation.
|
12
|
+
expect(validating_confirmation).to matcher
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'does not override the default message with a blank' do
|
16
|
-
validating_confirmation.
|
16
|
+
expect(validating_confirmation).to matcher.with_message(nil)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
context 'a model without a confirmation validation' do
|
21
21
|
it 'rejects' do
|
22
|
-
define_model(:example, :
|
22
|
+
expect(define_model(:example, attr: :string).new).not_to matcher
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
context 'a confirmation validation with a custom message' do
|
27
27
|
it 'accepts when the message matches' do
|
28
|
-
validating_confirmation(:
|
29
|
-
|
28
|
+
expect(validating_confirmation(message: 'custom')).
|
29
|
+
to matcher.with_message(/custom/)
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'rejects when the messages do not match' do
|
33
|
-
validating_confirmation(:
|
34
|
-
|
33
|
+
expect(validating_confirmation(message: 'custom')).
|
34
|
+
not_to matcher.with_message(/wrong/)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -40,7 +40,7 @@ describe Shoulda::Matchers::ActiveModel::ValidateConfirmationOfMatcher do
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def validating_confirmation(options = {})
|
43
|
-
define_model(:example, :
|
43
|
+
define_model(:example, attr: :string) do
|
44
44
|
validates_confirmation_of :attr, options
|
45
45
|
end.new
|
46
46
|
end
|
@@ -1,145 +1,330 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do
|
4
|
-
context '#description' do
|
5
|
-
it 'states that it allows only numeric values' do
|
6
|
-
matcher.description.should eq 'only allow numeric values for attr'
|
7
|
-
end
|
8
|
-
end
|
9
4
|
|
10
5
|
context 'with a model with a numericality validation' do
|
11
6
|
it 'accepts' do
|
12
|
-
validating_numericality.
|
7
|
+
expect(validating_numericality).to matcher
|
13
8
|
end
|
14
9
|
|
15
10
|
it 'does not override the default message with a blank' do
|
16
|
-
validating_numericality.
|
11
|
+
expect(validating_numericality).to matcher.with_message(nil)
|
17
12
|
end
|
18
13
|
end
|
19
14
|
|
20
15
|
context 'with a model without a numericality validation' do
|
21
16
|
it 'rejects' do
|
22
|
-
|
17
|
+
expect(not_validating_numericality).not_to matcher
|
23
18
|
end
|
24
19
|
|
25
20
|
it 'rejects with the ActiveRecord :not_a_number message' do
|
26
21
|
the_matcher = matcher
|
27
22
|
|
28
|
-
the_matcher.matches?(define_model(:example, :
|
23
|
+
the_matcher.matches?(define_model(:example, attr: :string).new)
|
29
24
|
|
30
|
-
the_matcher.failure_message_when_negated
|
25
|
+
expect(the_matcher.failure_message_when_negated)
|
26
|
+
.to include 'Did not expect errors to include "is not a number"'
|
31
27
|
end
|
32
28
|
|
33
29
|
it 'rejects with the ActiveRecord :not_an_integer message' do
|
34
30
|
the_matcher = matcher.only_integer
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
31
|
+
expect do
|
32
|
+
expect(not_validating_numericality).to the_matcher
|
33
|
+
end.to fail_with_message_including(
|
34
|
+
'Expected errors to include "must be an integer"'
|
35
|
+
)
|
39
36
|
end
|
40
37
|
|
41
38
|
it 'rejects with the ActiveRecord :odd message' do
|
42
39
|
the_matcher = matcher.odd
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
40
|
+
expect do
|
41
|
+
expect(not_validating_numericality).to the_matcher
|
42
|
+
end.to fail_with_message_including(
|
43
|
+
'Expected errors to include "must be odd"'
|
44
|
+
)
|
47
45
|
end
|
48
46
|
|
49
47
|
it 'rejects with the ActiveRecord :even message' do
|
50
48
|
the_matcher = matcher.even
|
49
|
+
expect do
|
50
|
+
expect(not_validating_numericality).to the_matcher
|
51
|
+
end.to fail_with_message_including(
|
52
|
+
'Expected errors to include "must be even"'
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
51
56
|
|
52
|
-
|
57
|
+
context 'with the allow_nil option' do
|
58
|
+
it 'allows nil values for that attribute' do
|
59
|
+
expect(validating_numericality(allow_nil: true)).to matcher.allow_nil
|
60
|
+
end
|
53
61
|
|
54
|
-
|
62
|
+
it 'rejects when the model does not allow nil' do
|
63
|
+
the_matcher = matcher.allow_nil
|
64
|
+
expect {
|
65
|
+
expect(validating_numericality).to the_matcher
|
66
|
+
}.to fail_with_message_including('Did not expect errors to include "is not a number"')
|
55
67
|
end
|
56
68
|
end
|
57
69
|
|
58
70
|
context 'with the only_integer option' do
|
59
71
|
it 'allows integer values for that attribute' do
|
60
|
-
validating_numericality(:
|
72
|
+
expect(validating_numericality(only_integer: true)).to matcher.only_integer
|
61
73
|
end
|
62
74
|
|
63
75
|
it 'rejects when the model does not enforce integer values' do
|
64
|
-
validating_numericality.
|
76
|
+
expect(validating_numericality).not_to matcher.only_integer
|
65
77
|
end
|
66
78
|
|
67
79
|
it 'rejects with the ActiveRecord :not_an_integer message' do
|
68
80
|
the_matcher = matcher.only_integer
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
81
|
+
expect do
|
82
|
+
expect(validating_numericality).to the_matcher
|
83
|
+
end.to fail_with_message_including(
|
84
|
+
'Expected errors to include "must be an integer"'
|
85
|
+
)
|
73
86
|
end
|
74
87
|
end
|
75
88
|
|
76
89
|
context 'with the odd option' do
|
77
90
|
it 'allows odd number values for that attribute' do
|
78
|
-
validating_numericality(:
|
91
|
+
expect(validating_numericality(odd: true)).to matcher.odd
|
79
92
|
end
|
80
93
|
|
81
94
|
it 'rejects when the model does not enforce odd number values' do
|
82
|
-
validating_numericality.
|
95
|
+
expect(validating_numericality).not_to matcher.odd
|
83
96
|
end
|
84
97
|
|
85
98
|
it 'rejects with the ActiveRecord :odd message' do
|
86
99
|
the_matcher = matcher.odd
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
100
|
+
expect do
|
101
|
+
expect(validating_numericality).to the_matcher
|
102
|
+
end.to fail_with_message_including(
|
103
|
+
'Expected errors to include "must be odd"'
|
104
|
+
)
|
91
105
|
end
|
92
106
|
end
|
93
107
|
|
94
108
|
context 'with the even option' do
|
95
109
|
it 'allows even number values for that attribute' do
|
96
|
-
validating_numericality(:
|
110
|
+
expect(validating_numericality(even: true)).to matcher.even
|
97
111
|
end
|
98
112
|
|
99
113
|
it 'rejects when the model does not enforce even number values' do
|
100
|
-
validating_numericality.
|
114
|
+
expect(validating_numericality).not_to matcher.even
|
101
115
|
end
|
102
116
|
|
103
117
|
it 'rejects with the ActiveRecord :even message' do
|
104
118
|
the_matcher = matcher.even
|
119
|
+
expect do
|
120
|
+
expect(validating_numericality).to the_matcher
|
121
|
+
end.to fail_with_message_including(
|
122
|
+
'Expected errors to include "must be even"'
|
123
|
+
)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'with multiple options together' do
|
128
|
+
context 'the success cases' do
|
129
|
+
it do
|
130
|
+
expect(validating_numericality(only_integer: true, greater_than: 18))
|
131
|
+
.to matcher.only_integer.is_greater_than(18)
|
132
|
+
end
|
133
|
+
|
134
|
+
it do
|
135
|
+
expect(validating_numericality(even: true, greater_than: 18))
|
136
|
+
.to matcher.even.is_greater_than(18)
|
137
|
+
end
|
138
|
+
it do
|
139
|
+
expect(validating_numericality(odd: true, less_than_or_equal_to: 99))
|
140
|
+
.to matcher.odd.is_less_than_or_equal_to(99)
|
141
|
+
end
|
142
|
+
|
143
|
+
it do
|
144
|
+
expect(validating_numericality(
|
145
|
+
only_integer: true,
|
146
|
+
greater_than: 18,
|
147
|
+
less_than: 99)
|
148
|
+
).to matcher.only_integer.is_greater_than(18).is_less_than(99)
|
149
|
+
end
|
150
|
+
end
|
105
151
|
|
106
|
-
|
152
|
+
context 'the failure cases with different validators' do
|
153
|
+
it do
|
154
|
+
expect(validating_numericality(even: true, greater_than: 18))
|
155
|
+
.not_to matcher.only_integer.is_greater_than(18)
|
156
|
+
end
|
157
|
+
|
158
|
+
it do
|
159
|
+
expect(validating_numericality(greater_than: 18))
|
160
|
+
.not_to matcher.only_integer.is_greater_than(18)
|
161
|
+
end
|
162
|
+
|
163
|
+
it do
|
164
|
+
expect(
|
165
|
+
validating_numericality(even: true, greater_than_or_equal_to: 18)
|
166
|
+
).not_to matcher.even.is_greater_than(18)
|
167
|
+
end
|
168
|
+
|
169
|
+
it do
|
170
|
+
expect(validating_numericality(odd: true, greater_than: 18))
|
171
|
+
.not_to matcher.even.is_greater_than(18)
|
172
|
+
end
|
173
|
+
|
174
|
+
it do
|
175
|
+
expect(validating_numericality(
|
176
|
+
odd: true,
|
177
|
+
greater_than_or_equal_to: 99
|
178
|
+
)
|
179
|
+
).not_to matcher.odd.is_less_than_or_equal_to(99)
|
180
|
+
end
|
181
|
+
|
182
|
+
it do
|
183
|
+
expect(validating_numericality(
|
184
|
+
only_integer: true,
|
185
|
+
greater_than_or_equal_to: 18,
|
186
|
+
less_than: 99
|
187
|
+
)
|
188
|
+
).not_to matcher.only_integer.is_greater_than(18).is_less_than(99)
|
189
|
+
end
|
190
|
+
end
|
107
191
|
|
108
|
-
|
192
|
+
context 'the failure cases with wrong values' do
|
193
|
+
it do
|
194
|
+
expect(validating_numericality(only_integer: true, greater_than: 19))
|
195
|
+
.not_to matcher.only_integer.is_greater_than(18)
|
196
|
+
end
|
197
|
+
|
198
|
+
it do
|
199
|
+
expect(validating_numericality(only_integer: true, greater_than: 17))
|
200
|
+
.not_to matcher.only_integer.is_greater_than(18)
|
201
|
+
end
|
202
|
+
|
203
|
+
it do
|
204
|
+
expect(validating_numericality(even: true, greater_than: 20))
|
205
|
+
.not_to matcher.even.is_greater_than(18)
|
206
|
+
end
|
207
|
+
|
208
|
+
it do
|
209
|
+
expect(validating_numericality(even: true, greater_than: 16))
|
210
|
+
.not_to matcher.even.is_greater_than(18)
|
211
|
+
end
|
212
|
+
|
213
|
+
it do
|
214
|
+
expect(validating_numericality(odd: true, less_than_or_equal_to: 101))
|
215
|
+
.not_to matcher.odd.is_less_than_or_equal_to(99)
|
216
|
+
end
|
217
|
+
|
218
|
+
it do
|
219
|
+
expect(validating_numericality(odd: true, less_than_or_equal_to: 97))
|
220
|
+
.not_to matcher.odd.is_less_than_or_equal_to(99)
|
221
|
+
end
|
222
|
+
|
223
|
+
it do
|
224
|
+
expect(validating_numericality(only_integer: true,
|
225
|
+
greater_than: 19,
|
226
|
+
less_than: 99))
|
227
|
+
.not_to matcher.only_integer.is_greater_than(18).is_less_than(99)
|
228
|
+
end
|
229
|
+
|
230
|
+
it do
|
231
|
+
expect(validating_numericality(only_integer: true,
|
232
|
+
greater_than: 18,
|
233
|
+
less_than: 100))
|
234
|
+
.not_to matcher.only_integer.is_greater_than(18).is_less_than(99)
|
235
|
+
end
|
109
236
|
end
|
110
237
|
end
|
111
238
|
|
112
239
|
context 'with a custom validation message' do
|
113
240
|
it 'accepts when the messages match' do
|
114
|
-
validating_numericality(:
|
115
|
-
|
241
|
+
expect(validating_numericality(message: 'custom')).
|
242
|
+
to matcher.with_message(/custom/)
|
116
243
|
end
|
117
244
|
|
118
245
|
it 'rejects when the messages do not match' do
|
119
|
-
validating_numericality(:
|
120
|
-
|
246
|
+
expect(validating_numericality(message: 'custom')).
|
247
|
+
not_to matcher.with_message(/wrong/)
|
121
248
|
end
|
122
249
|
end
|
123
250
|
|
124
251
|
context 'when the subject is stubbed' do
|
125
252
|
it 'retains stubs on submatchers' do
|
126
|
-
subject = define_model :example, :
|
127
|
-
validates_numericality_of :attr, :
|
253
|
+
subject = define_model :example, attr: :string do
|
254
|
+
validates_numericality_of :attr, odd: true
|
128
255
|
before_validation :set_attr!
|
129
256
|
def set_attr!; self.attr = 5 end
|
130
257
|
end.new
|
131
258
|
|
132
259
|
subject.stubs(:set_attr!)
|
133
|
-
subject.
|
260
|
+
expect(subject).to matcher.odd
|
134
261
|
end
|
135
262
|
end
|
136
263
|
|
264
|
+
describe '#description' do
|
265
|
+
context 'without submatchers' do
|
266
|
+
it { expect(matcher.description).to eq 'only allow numbers for attr' }
|
267
|
+
end
|
268
|
+
|
269
|
+
context 'with only integer option' do
|
270
|
+
it do
|
271
|
+
expect(matcher.only_integer.description)
|
272
|
+
.to eq 'only allow integers for attr'
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
[:odd, :even].each do |type|
|
277
|
+
context "with #{type} option" do
|
278
|
+
it do
|
279
|
+
expect(matcher.__send__(type).description)
|
280
|
+
.to eq "only allow #{type} numbers for attr"
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
[:is_greater_than,
|
286
|
+
:is_greater_than_or_equal_to,
|
287
|
+
:is_less_than,
|
288
|
+
:is_less_than_or_equal_to,
|
289
|
+
:is_equal_to ].each do |comparison|
|
290
|
+
context "with #{comparison} option" do
|
291
|
+
it do
|
292
|
+
expect(matcher.__send__(comparison, 18).description)
|
293
|
+
.to eq(
|
294
|
+
'only allow numbers for attr which are ' +
|
295
|
+
"#{comparison.to_s.sub('is_', '').gsub('_', ' ')} 18"
|
296
|
+
)
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
context 'with odd, is_greater_than_or_equal_to option' do
|
302
|
+
it do
|
303
|
+
expect(matcher.odd.is_greater_than_or_equal_to(18).description)
|
304
|
+
.to eq(
|
305
|
+
'only allow odd numbers for attr ' +
|
306
|
+
'which are greater than or equal to 18'
|
307
|
+
)
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
context 'with only integer, is_greater_than and less_than_or_equal_to option' do
|
312
|
+
it { expect(matcher.only_integer.is_greater_than(18).is_less_than_or_equal_to(100).description).
|
313
|
+
to eq "only allow integers for attr which are greater than 18 and less than or equal to 100" }
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
|
137
318
|
def validating_numericality(options = {})
|
138
|
-
define_model :example, :
|
319
|
+
define_model :example, attr: :string do
|
139
320
|
validates_numericality_of :attr, options
|
140
321
|
end.new
|
141
322
|
end
|
142
323
|
|
324
|
+
def not_validating_numericality
|
325
|
+
define_model(:example, attr: :string).new
|
326
|
+
end
|
327
|
+
|
143
328
|
def matcher
|
144
329
|
validate_numericality_of(:attr)
|
145
330
|
end
|