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