shoulda-matchers 1.4.2 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +11 -3
- data/Appraisals +3 -3
- data/Gemfile +1 -1
- data/Gemfile.lock +69 -70
- data/MIT-LICENSE +1 -1
- data/NEWS.md +22 -4
- data/README.md +3 -1
- data/Rakefile +9 -5
- data/features/step_definitions/rails_steps.rb +7 -7
- data/features/support/env.rb +1 -1
- data/gemfiles/3.0.gemfile +1 -1
- data/gemfiles/3.0.gemfile.lock +25 -22
- data/gemfiles/3.1.gemfile +1 -1
- data/gemfiles/3.1.gemfile.lock +27 -24
- data/gemfiles/3.2.gemfile +1 -1
- data/gemfiles/3.2.gemfile.lock +56 -52
- data/lib/shoulda/matchers/action_controller.rb +3 -2
- data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +10 -8
- data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +2 -4
- data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +3 -4
- data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +3 -4
- data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +6 -6
- data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +5 -2
- data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +2 -2
- data/lib/shoulda/matchers/action_controller/route_matcher.rb +12 -12
- data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +3 -4
- data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +10 -11
- data/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +121 -0
- data/lib/shoulda/matchers/action_mailer/have_sent_email_matcher.rb +7 -4
- data/lib/shoulda/matchers/active_model.rb +1 -1
- data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +8 -8
- data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +5 -4
- data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +3 -3
- data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +2 -2
- data/lib/shoulda/matchers/active_model/exception_message_finder.rb +0 -1
- data/lib/shoulda/matchers/active_model/helpers.rb +16 -7
- data/lib/shoulda/matchers/active_model/only_integer_matcher.rb +3 -3
- data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +8 -6
- data/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb +19 -10
- data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +4 -4
- data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +19 -21
- data/lib/shoulda/matchers/active_model/validation_matcher.rb +7 -7
- data/lib/shoulda/matchers/active_model/validation_message_finder.rb +1 -1
- data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +5 -5
- data/lib/shoulda/matchers/active_record/association_matcher.rb +3 -3
- data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +11 -7
- data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +2 -2
- data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +5 -5
- data/lib/shoulda/matchers/active_record/query_the_database_matcher.rb +5 -2
- data/lib/shoulda/matchers/active_record/serialize_matcher.rb +3 -3
- data/lib/shoulda/matchers/assertion_error.rb +4 -1
- data/lib/shoulda/matchers/independent/delegate_matcher.rb +17 -7
- data/lib/shoulda/matchers/integrations/rspec.rb +19 -33
- data/lib/shoulda/matchers/version.rb +1 -1
- data/shoulda-matchers.gemspec +6 -4
- data/spec/shoulda/matchers/action_controller/assign_to_matcher_spec.rb +66 -0
- data/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +22 -0
- data/spec/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +42 -0
- data/spec/shoulda/matchers/action_controller/render_template_matcher_spec.rb +78 -0
- data/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +58 -0
- data/spec/shoulda/matchers/action_controller/respond_with_content_type_matcher_spec.rb +31 -0
- data/spec/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +31 -0
- data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +65 -0
- data/spec/shoulda/matchers/action_controller/set_session_matcher_spec.rb +51 -0
- data/spec/shoulda/matchers/action_controller/set_the_flash_matcher_spec.rb +153 -0
- data/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +142 -0
- data/spec/shoulda/matchers/action_mailer/have_sent_email_spec.rb +324 -0
- data/spec/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +111 -0
- data/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +124 -0
- data/spec/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +43 -0
- data/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb +74 -0
- data/spec/shoulda/matchers/active_model/ensure_inclusion_of_matcher_spec.rb +171 -0
- data/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb +113 -0
- data/spec/shoulda/{active_model → matchers/active_model}/exception_message_finder_spec.rb +2 -2
- data/spec/shoulda/matchers/active_model/helpers_spec.rb +158 -0
- data/spec/shoulda/matchers/active_model/only_integer_matcher_spec.rb +52 -0
- data/spec/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +41 -0
- data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +41 -0
- data/spec/shoulda/matchers/active_model/validate_format_of_matcher_spec.rb +75 -0
- data/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +73 -0
- data/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +127 -0
- data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +175 -0
- data/spec/shoulda/{active_model → matchers/active_model}/validation_message_finder_spec.rb +0 -1
- data/spec/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +106 -0
- data/spec/shoulda/matchers/active_record/association_matcher_spec.rb +534 -0
- data/spec/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +111 -0
- data/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +78 -0
- data/spec/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +41 -0
- data/spec/shoulda/{active_record → matchers/active_record}/query_the_database_matcher_spec.rb +10 -10
- data/spec/shoulda/matchers/active_record/serialize_matcher_spec.rb +86 -0
- data/spec/shoulda/{independent → matchers/independent}/delegate_matcher_spec.rb +55 -34
- data/spec/spec_helper.rb +1 -2
- data/spec/support/active_model_versions.rb +4 -0
- data/spec/support/activemodel_helpers.rb +19 -0
- data/spec/support/controller_builder.rb +38 -12
- data/spec/support/model_builder.rb +8 -2
- metadata +106 -98
- data/spec/fixtures/addresses.yml +0 -3
- data/spec/fixtures/friendships.yml +0 -0
- data/spec/fixtures/posts.yml +0 -5
- data/spec/fixtures/products.yml +0 -0
- data/spec/fixtures/taggings.yml +0 -0
- data/spec/fixtures/tags.yml +0 -9
- data/spec/fixtures/users.yml +0 -6
- data/spec/shoulda/action_controller/assign_to_matcher_spec.rb +0 -63
- data/spec/shoulda/action_controller/filter_param_matcher_spec.rb +0 -20
- data/spec/shoulda/action_controller/redirect_to_matcher_spec.rb +0 -40
- data/spec/shoulda/action_controller/render_template_matcher_spec.rb +0 -69
- data/spec/shoulda/action_controller/render_with_layout_matcher_spec.rb +0 -55
- data/spec/shoulda/action_controller/respond_with_content_type_matcher_spec.rb +0 -28
- data/spec/shoulda/action_controller/respond_with_matcher_spec.rb +0 -83
- data/spec/shoulda/action_controller/route_matcher_spec.rb +0 -65
- data/spec/shoulda/action_controller/set_session_matcher_spec.rb +0 -46
- data/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb +0 -130
- data/spec/shoulda/action_mailer/have_sent_email_spec.rb +0 -333
- data/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb +0 -115
- data/spec/shoulda/active_model/allow_value_matcher_spec.rb +0 -131
- data/spec/shoulda/active_model/disallow_value_matcher_spec.rb +0 -65
- data/spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb +0 -79
- data/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +0 -181
- data/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb +0 -138
- data/spec/shoulda/active_model/helpers_spec.rb +0 -129
- data/spec/shoulda/active_model/only_integer_matcher_spec.rb +0 -69
- data/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb +0 -43
- data/spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb +0 -48
- data/spec/shoulda/active_model/validate_format_of_matcher_spec.rb +0 -79
- data/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb +0 -112
- data/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +0 -135
- data/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb +0 -154
- data/spec/shoulda/active_record/accept_nested_attributes_for_matcher_spec.rb +0 -84
- data/spec/shoulda/active_record/association_matcher_spec.rb +0 -642
- data/spec/shoulda/active_record/have_db_column_matcher_spec.rb +0 -185
- data/spec/shoulda/active_record/have_db_index_matcher_spec.rb +0 -105
- data/spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb +0 -46
- data/spec/shoulda/active_record/serialize_matcher_spec.rb +0 -81
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveModel::EnsureLengthOfMatcher do
|
4
|
+
context 'an attribute with a non-zero minimum length validation' do
|
5
|
+
it 'accepts ensuring the correct minimum length' do
|
6
|
+
validating_length(:minimum => 4).
|
7
|
+
should ensure_length_of(:attr).is_at_least(4)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'rejects ensuring a lower minimum length with any message' do
|
11
|
+
validating_length(:minimum => 4).
|
12
|
+
should_not ensure_length_of(:attr).is_at_least(3).with_short_message(/.*/)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'rejects ensuring a higher minimum length with any message' do
|
16
|
+
validating_length(:minimum => 4).
|
17
|
+
should_not ensure_length_of(:attr).is_at_least(5).with_short_message(/.*/)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'does not override the default message with a blank' do
|
21
|
+
validating_length(:minimum => 4).
|
22
|
+
should ensure_length_of(:attr).is_at_least(4).with_short_message(nil)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'an attribute with a minimum length validation of 0' do
|
27
|
+
it 'accepts ensuring the correct minimum length' do
|
28
|
+
validating_length(:minimum => 0).
|
29
|
+
should ensure_length_of(:attr).is_at_least(0)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'an attribute with a maximum length' do
|
34
|
+
it 'accepts ensuring the correct maximum length' do
|
35
|
+
validating_length(:maximum => 4).
|
36
|
+
should ensure_length_of(:attr).is_at_most(4)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'rejects ensuring a lower maximum length with any message' do
|
40
|
+
validating_length(:maximum => 4).
|
41
|
+
should_not ensure_length_of(:attr).is_at_most(3).with_long_message(/.*/)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'rejects ensuring a higher maximum length with any message' do
|
45
|
+
validating_length(:maximum => 4).
|
46
|
+
should_not ensure_length_of(:attr).is_at_most(5).with_long_message(/.*/)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'does not override the default message with a blank' do
|
50
|
+
validating_length(:maximum => 4).
|
51
|
+
should ensure_length_of(:attr).is_at_most(4).with_long_message(nil)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'an attribute with a required exact length' do
|
56
|
+
it 'accepts ensuring the correct length' do
|
57
|
+
validating_length(:is => 4).should ensure_length_of(:attr).is_equal_to(4)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'rejects ensuring a lower maximum length with any message' do
|
61
|
+
validating_length(:is => 4).
|
62
|
+
should_not ensure_length_of(:attr).is_equal_to(3).with_message(/.*/)
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'rejects ensuring a higher maximum length with any message' do
|
66
|
+
validating_length(:is => 4).
|
67
|
+
should_not ensure_length_of(:attr).is_equal_to(5).with_message(/.*/)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'does not override the default message with a blank' do
|
71
|
+
validating_length(:is => 4).
|
72
|
+
should ensure_length_of(:attr).is_equal_to(4).with_message(nil)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'an attribute with a required exact length and another validation' do
|
77
|
+
it 'accepts ensuring the correct length' do
|
78
|
+
model = define_model(:example, :attr => :string) do
|
79
|
+
validates_length_of :attr, :is => 4
|
80
|
+
validates_numericality_of :attr
|
81
|
+
end.new
|
82
|
+
|
83
|
+
model.should ensure_length_of(:attr).is_equal_to(4)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'an attribute with a custom minimum length validation' do
|
88
|
+
it 'accepts ensuring the correct minimum length' do
|
89
|
+
validating_length(:minimum => 4, :too_short => 'foobar').
|
90
|
+
should ensure_length_of(:attr).is_at_least(4).with_short_message(/foo/)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'an attribute with a custom maximum length validation' do
|
95
|
+
it 'accepts ensuring the correct minimum length' do
|
96
|
+
validating_length(:maximum => 4, :too_long => 'foobar').
|
97
|
+
should ensure_length_of(:attr).is_at_most(4).with_long_message(/foo/)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'an attribute without a length validation' do
|
102
|
+
it 'rejects ensuring a minimum length' do
|
103
|
+
define_model(:example, :attr => :string).new.
|
104
|
+
should_not ensure_length_of(:attr).is_at_least(1)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def validating_length(options = {})
|
109
|
+
define_model(:example, :attr => :string) do
|
110
|
+
validates_length_of :attr, options
|
111
|
+
end.new
|
112
|
+
end
|
113
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Shoulda::Matchers::ActiveModel::ExceptionMessageFinder do
|
4
|
-
if
|
4
|
+
if active_model_3_2?
|
5
5
|
context '#allow_description' do
|
6
6
|
it 'describes its attribute' do
|
7
7
|
finder = build_finder(:attribute => :attr)
|
8
8
|
|
9
9
|
description = finder.allow_description('allowed values')
|
10
10
|
|
11
|
-
description.should ==
|
11
|
+
description.should == %q(doesn't raise when attr is set to allowed values)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -0,0 +1,158 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Shoulda::Matchers::ActiveModel::Helpers do
|
5
|
+
include Shoulda::Matchers::ActiveModel
|
6
|
+
after { I18n.backend.reload! }
|
7
|
+
|
8
|
+
describe 'default_error_message' do
|
9
|
+
context 'if the translation for the model attribute’s error exists' do
|
10
|
+
it 'provides the right error message for validate_presence_of' do
|
11
|
+
store_translations
|
12
|
+
|
13
|
+
assert_presence_validation_has_correct_message
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'provides the right error message for validates_length_of' do
|
17
|
+
store_translations
|
18
|
+
|
19
|
+
assert_length_validation_has_correct_message
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'if no translation for the model attribute’s error exists' do
|
24
|
+
context 'and the translation for the model’s error exists' do
|
25
|
+
it 'provides the right error message for validate_presence_of' do
|
26
|
+
store_translations(:without => :model_attribute)
|
27
|
+
|
28
|
+
assert_presence_validation_has_correct_message
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'provides the right error message for validates_length_of' do
|
32
|
+
store_translations(:without => :model_attribute)
|
33
|
+
|
34
|
+
assert_length_validation_has_correct_message
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'and no translation for the model’s error exists' do
|
39
|
+
context 'and the translation for the message exists' do
|
40
|
+
it 'provides the right error message for validate_presence_of' do
|
41
|
+
store_translations(:without => [:model_attribute, :model])
|
42
|
+
|
43
|
+
assert_presence_validation_has_correct_message
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'provides the right error message for validates_length_of' do
|
47
|
+
store_translations(:without => [:model_attribute, :model])
|
48
|
+
|
49
|
+
assert_length_validation_has_correct_message
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'and no translation for the message exists' do
|
54
|
+
context 'and the translation for the attribute exists' do
|
55
|
+
it 'provides the right error message for validate_presence_of' do
|
56
|
+
store_translations(:without => [:model_attribute, :model, :message])
|
57
|
+
|
58
|
+
assert_presence_validation_has_correct_message
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'provides the right error message for validates_length_of' do
|
62
|
+
store_translations(:without => [:model_attribute, :model, :message])
|
63
|
+
|
64
|
+
assert_length_validation_has_correct_message
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'and no translation for the attribute exists' do
|
69
|
+
it 'provides the general error message for validate_presence_of' do
|
70
|
+
assert_presence_validation_has_correct_message
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'provides the general error message for validates_length_of' do
|
74
|
+
assert_length_validation_has_correct_message
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
if active_model_3_0?
|
82
|
+
context 'if ActiveModel::Errors#generate_message behavior has changed' do
|
83
|
+
it 'provides the right error message for validate_presence_of' do
|
84
|
+
stub_active_model_message_generation(:type => :blank,
|
85
|
+
:message => 'Behavior has diverged.')
|
86
|
+
assert_presence_validation_has_correct_message
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def assert_presence_validation_has_correct_message
|
93
|
+
define_model :example, :attr => :string do
|
94
|
+
validates_presence_of :attr
|
95
|
+
end.new.should validate_presence_of(:attr)
|
96
|
+
end
|
97
|
+
|
98
|
+
def assert_length_validation_has_correct_message
|
99
|
+
define_model :example, :attr => :string do
|
100
|
+
validates_length_of :attr, :is => 40, :allow_blank => true
|
101
|
+
end.new.should ensure_length_of(:attr).is_equal_to(40)
|
102
|
+
end
|
103
|
+
|
104
|
+
def store_translations(options = {:without => []})
|
105
|
+
options[:without] = Array.wrap(options[:without] || [])
|
106
|
+
|
107
|
+
translations = {
|
108
|
+
:activerecord => {
|
109
|
+
:errors => {
|
110
|
+
:models => {
|
111
|
+
:example => {
|
112
|
+
:attributes => {
|
113
|
+
:attr => {}
|
114
|
+
}
|
115
|
+
}
|
116
|
+
},
|
117
|
+
:messages => {}
|
118
|
+
}
|
119
|
+
},
|
120
|
+
:errors => {
|
121
|
+
:attributes => {
|
122
|
+
:attr => {}
|
123
|
+
},
|
124
|
+
:messages => {}
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
unless options[:without].include?(:model_attribute)
|
129
|
+
translations[:activerecord][:errors][:models][:example][:attributes][:attr][:blank] = "Don't you do that to me!"
|
130
|
+
translations[:activerecord][:errors][:models][:example][:attributes][:attr][:wrong_length] = "Don't you do that to me!"
|
131
|
+
end
|
132
|
+
|
133
|
+
unless options[:without].include?(:model)
|
134
|
+
translations[:activerecord][:errors][:models][:example][:blank] = 'Give it one more try!'
|
135
|
+
translations[:activerecord][:errors][:models][:example][:wrong_length] = 'Give it one more try!'
|
136
|
+
end
|
137
|
+
|
138
|
+
unless options[:without].include?(:message)
|
139
|
+
translations[:activerecord][:errors][:messages][:blank] = 'Oh no!'
|
140
|
+
translations[:activerecord][:errors][:messages][:wrong_length] = 'Oh no!'
|
141
|
+
end
|
142
|
+
|
143
|
+
unless options[:without].include?(:attribute)
|
144
|
+
translations[:errors][:attributes][:attr][:blank] = 'Seriously?'
|
145
|
+
translations[:errors][:attributes][:attr][:wrong_length] = 'Seriously?'
|
146
|
+
end
|
147
|
+
|
148
|
+
I18n.backend.store_translations(:en, translations)
|
149
|
+
end
|
150
|
+
|
151
|
+
def stub_active_model_message_generation(options = {})
|
152
|
+
attribute = options.delete(:attribute) || :attr
|
153
|
+
message = options.delete(:message)
|
154
|
+
type = options.delete(:type)
|
155
|
+
|
156
|
+
ActiveModel::Errors.any_instance.expects(:generate_message).with(attribute, type, {}).at_least_once.returns(message)
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveModel::OnlyIntegerMatcher do
|
4
|
+
context 'given an attribute that only allows integer values' do
|
5
|
+
it 'matches' do
|
6
|
+
only_integer.should new_matcher
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'allows integer types' do
|
10
|
+
new_matcher.allowed_types.should == 'integer'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns itself when given a message' do
|
14
|
+
matcher = new_matcher
|
15
|
+
matcher.with_message('some message').should == matcher
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'given an attribute that only allows integer values with a custom validation message' do
|
20
|
+
it 'only accepts integer values for that attribute with that message' do
|
21
|
+
only_integer(:message => 'custom').should new_matcher.with_message(/custom/)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'rejects integer values for that attribute with another message' do
|
25
|
+
only_integer(:message => 'custom').should_not new_matcher.with_message(/wrong/)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when the model does not have an only_integer validation' do
|
30
|
+
it 'does not match' do
|
31
|
+
define_model(:example, :attr => :string).new.should_not new_matcher
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'fails with the ActiveRecord :not_an_integer message' do
|
35
|
+
matcher = new_matcher
|
36
|
+
|
37
|
+
matcher.matches?(define_model(:example, :attr => :string).new)
|
38
|
+
|
39
|
+
matcher.failure_message_for_should.should include 'Expected errors to include "must be an integer"'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def new_matcher
|
44
|
+
described_class.new(:attr)
|
45
|
+
end
|
46
|
+
|
47
|
+
def only_integer(options = {})
|
48
|
+
define_model :example, :attr => :string do
|
49
|
+
validates_numericality_of :attr, { :only_integer => true }.merge(options)
|
50
|
+
end.new
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveModel::ValidateAcceptanceOfMatcher do
|
4
|
+
context 'a model with an acceptance validation' do
|
5
|
+
it 'accepts when the attributes match' do
|
6
|
+
validating_acceptance.should matcher
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'does not overwrite the default message with nil' do
|
10
|
+
validating_acceptance.should matcher.with_message(nil)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'a model without an acceptance validation' do
|
15
|
+
it 'rejects' do
|
16
|
+
define_model(:example, :attr => :string).new.should_not matcher
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'an attribute which must be accepted with a custom message' do
|
21
|
+
it 'accepts when the message matches' do
|
22
|
+
validating_acceptance(:message => 'custom').
|
23
|
+
should matcher.with_message(/custom/)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'rejects when the message does not match' do
|
27
|
+
validating_acceptance(:message => 'custom').
|
28
|
+
should_not matcher.with_message(/wrong/)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def matcher
|
33
|
+
validate_acceptance_of(:attr)
|
34
|
+
end
|
35
|
+
|
36
|
+
def validating_acceptance(options = {})
|
37
|
+
define_model(:example, :attr => :string) do
|
38
|
+
validates_acceptance_of :attr, options
|
39
|
+
end.new
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveModel::ValidateConfirmationOfMatcher do
|
4
|
+
context 'a model with a confirmation validation' do
|
5
|
+
it 'accepts' do
|
6
|
+
validating_confirmation.should matcher
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'does not override the default message with a blank' do
|
10
|
+
validating_confirmation.should matcher.with_message(nil)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'a model without a confirmation validation' do
|
15
|
+
it 'rejects' do
|
16
|
+
define_model(:example, :attr => :string).new.should_not matcher
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'a confirmation validation with a custom message' do
|
21
|
+
it 'accepts when the message matches' do
|
22
|
+
validating_confirmation(:message => 'custom').
|
23
|
+
should matcher.with_message(/custom/)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'rejects when the messages do not match' do
|
27
|
+
validating_confirmation(:message => 'custom').
|
28
|
+
should_not matcher.with_message(/wrong/)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def matcher
|
33
|
+
validate_confirmation_of(:attr)
|
34
|
+
end
|
35
|
+
|
36
|
+
def validating_confirmation(options = {})
|
37
|
+
define_model(:example, :attr => :string) do
|
38
|
+
validates_confirmation_of :attr, options
|
39
|
+
end.new
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveModel::ValidateFormatOfMatcher do
|
4
|
+
context 'a model with a format validation' do
|
5
|
+
it 'accepts when format matches ' do
|
6
|
+
validating_format(:with => /^\d{5}$/).should matcher.with('12345')
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'rejects blank with should_not' do
|
10
|
+
validating_format(:with => /^\d{5}$/).should_not matcher.with(' ')
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'rejects blank with not_with' do
|
14
|
+
validating_format(:with => /^\d{5}$/).should matcher.not_with(' ')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'rejects nil' do
|
18
|
+
validating_format(:with => /^\d{5}$/).should_not matcher.with(nil)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'rejects a non-matching format with should_not' do
|
22
|
+
validating_format(:with => /^\d{5}$/).should_not matcher.with('1234a')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'rejects a non-matching format with not_with' do
|
26
|
+
validating_format(:with => /^\d{5}$/).should matcher.not_with('1234a')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'raises an error if you try to call both with and not_with' do
|
30
|
+
expect {
|
31
|
+
validate_format_of(:attr).not_with('123456').with('12345')
|
32
|
+
}.to raise_error(RuntimeError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when allow_blank or allow_nil are set' do
|
37
|
+
it 'is valid when attr is nil' do
|
38
|
+
validating_format(:with => /abc/, :allow_nil => true).
|
39
|
+
should matcher.with(nil)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'is valid when attr is blank' do
|
43
|
+
validating_format(:with => /abc/, :allow_blank => true).
|
44
|
+
should matcher.with(' ')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context '#allow_blank' do
|
49
|
+
it 'accepts when allow_blank matches' do
|
50
|
+
validating_format(:with => /abc/, :allow_blank => true).
|
51
|
+
should matcher.allow_blank
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'rejects when allow_blank does not match' do
|
55
|
+
validating_format(:with => /abc/, :allow_blank => false).
|
56
|
+
should_not matcher.allow_blank
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context '#allow_nil' do
|
61
|
+
it 'accepts when allow_nil matches' do
|
62
|
+
validating_format(:with => /abc/, :allow_nil => true).
|
63
|
+
should matcher.allow_nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'rejects when allow_nil does not match' do
|
67
|
+
validating_format(:with => /abc/, :allow_nil => false).
|
68
|
+
should_not matcher.allow_nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def matcher
|
73
|
+
validate_format_of(:attr)
|
74
|
+
end
|
75
|
+
end
|