shoulda 2.0.6 → 2.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/README.rdoc +35 -7
  2. data/Rakefile +5 -3
  3. data/lib/shoulda.rb +7 -15
  4. data/lib/shoulda/action_mailer.rb +1 -1
  5. data/lib/shoulda/action_mailer/assertions.rb +32 -33
  6. data/lib/shoulda/active_record.rb +6 -2
  7. data/lib/shoulda/active_record/assertions.rb +62 -81
  8. data/lib/shoulda/active_record/helpers.rb +40 -0
  9. data/lib/shoulda/active_record/macros.rb +518 -639
  10. data/lib/shoulda/active_record/matchers.rb +42 -0
  11. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  12. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
  13. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  14. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  15. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  16. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  17. data/lib/shoulda/active_record/matchers/have_index_matcher.rb +105 -0
  18. data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +125 -0
  19. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
  20. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
  21. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
  22. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  23. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  24. data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
  25. data/lib/shoulda/assertions.rb +50 -40
  26. data/lib/shoulda/autoload_macros.rb +46 -0
  27. data/lib/shoulda/context.rb +124 -126
  28. data/lib/shoulda/controller.rb +8 -8
  29. data/lib/shoulda/controller/formats/html.rb +158 -160
  30. data/lib/shoulda/controller/formats/xml.rb +132 -134
  31. data/lib/shoulda/controller/helpers.rb +51 -53
  32. data/lib/shoulda/controller/macros.rb +278 -258
  33. data/lib/shoulda/controller/resource_options.rb +211 -214
  34. data/lib/shoulda/helpers.rb +5 -7
  35. data/lib/shoulda/macros.rb +63 -64
  36. data/lib/shoulda/private_helpers.rb +16 -18
  37. data/lib/shoulda/rails.rb +1 -8
  38. data/lib/shoulda/rspec.rb +5 -0
  39. data/lib/shoulda/tasks/list_tests.rake +6 -1
  40. data/lib/shoulda/test_unit.rb +19 -0
  41. data/rails/init.rb +1 -1
  42. data/test/README +2 -2
  43. data/test/fail_macros.rb +16 -16
  44. data/test/functional/posts_controller_test.rb +5 -2
  45. data/test/matchers/allow_mass_assignment_of_matcher_test.rb +68 -0
  46. data/test/matchers/allow_value_matcher_test.rb +41 -0
  47. data/test/matchers/association_matcher_test.rb +258 -0
  48. data/test/matchers/ensure_inclusion_of_matcher_test.rb +80 -0
  49. data/test/matchers/ensure_length_of_matcher_test.rb +158 -0
  50. data/test/matchers/have_db_column_matcher_test.rb +169 -0
  51. data/test/matchers/have_index_matcher_test.rb +74 -0
  52. data/test/matchers/have_named_scope_matcher_test.rb +65 -0
  53. data/test/matchers/have_readonly_attributes_matcher_test.rb +29 -0
  54. data/test/matchers/validate_acceptance_of_matcher_test.rb +44 -0
  55. data/test/matchers/validate_numericality_of_matcher_test.rb +52 -0
  56. data/test/matchers/validate_presence_of_matcher_test.rb +86 -0
  57. data/test/matchers/validate_uniqueness_of_matcher_test.rb +141 -0
  58. data/test/model_builder.rb +61 -0
  59. data/test/other/autoload_macro_test.rb +18 -0
  60. data/test/other/helpers_test.rb +58 -0
  61. data/test/other/private_helpers_test.rb +1 -1
  62. data/test/other/should_test.rb +16 -16
  63. data/test/rails_root/app/controllers/posts_controller.rb +6 -5
  64. data/test/rails_root/app/models/pets/dog.rb +10 -0
  65. data/test/rails_root/app/models/treat.rb +3 -0
  66. data/test/rails_root/app/models/user.rb +2 -2
  67. data/test/rails_root/app/views/layouts/posts.rhtml +2 -0
  68. data/test/rails_root/config/database.yml +1 -1
  69. data/test/rails_root/config/environments/{sqlite3.rb → test.rb} +0 -0
  70. data/test/rails_root/db/migrate/001_create_users.rb +3 -2
  71. data/test/rails_root/db/migrate/011_create_treats.rb +12 -0
  72. data/test/rails_root/log/test.log +0 -0
  73. data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
  74. data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  75. data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  76. data/test/test_helper.rb +3 -1
  77. data/test/unit/address_test.rb +1 -1
  78. data/test/unit/dog_test.rb +5 -2
  79. data/test/unit/post_test.rb +7 -3
  80. data/test/unit/product_test.rb +2 -2
  81. data/test/unit/tag_test.rb +2 -1
  82. data/test/unit/user_test.rb +17 -8
  83. metadata +44 -4
  84. data/test/rails_root/app/models/dog.rb +0 -5
@@ -0,0 +1,258 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class AssociationMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "belong_to" do
6
+ setup do
7
+ @matcher = belong_to(:parent)
8
+ end
9
+
10
+ should "accept a good association with the default foreign key" do
11
+ define_model :parent
12
+ define_model :child, :parent_id => :integer do
13
+ belongs_to :parent
14
+ end
15
+ assert_accepts @matcher, Child.new
16
+ end
17
+
18
+ should "reject a nonexistent association" do
19
+ define_model :child
20
+ assert_rejects @matcher, Child.new
21
+ end
22
+
23
+ should "reject an association of the wrong type" do
24
+ define_model :parent, :child_id => :integer
25
+ child_class = define_model :child do
26
+ has_one :parent
27
+ end
28
+ assert_rejects @matcher, Child.new
29
+ end
30
+
31
+ should "reject an association that has a nonexistent foreign key" do
32
+ define_model :parent
33
+ define_model :child do
34
+ belongs_to :parent
35
+ end
36
+ assert_rejects @matcher, Child.new
37
+ end
38
+
39
+ should "accept an association with an existing custom foreign key" do
40
+ define_model :parent
41
+ define_model :child, :guardian_id => :integer do
42
+ belongs_to :parent, :foreign_key => 'guardian_id'
43
+ end
44
+ assert_accepts @matcher, Child.new
45
+ end
46
+
47
+ should "accept a polymorphic association" do
48
+ define_model :child, :parent_type => :string,
49
+ :parent_id => :integer do
50
+ belongs_to :parent, :polymorphic => true
51
+ end
52
+ assert_accepts @matcher, Child.new
53
+ end
54
+
55
+ should "accept an association with a valid :dependent option" do
56
+ define_model :parent
57
+ define_model :child, :parent_id => :integer do
58
+ belongs_to :parent, :dependent => :destroy
59
+ end
60
+ assert_accepts @matcher.dependent(:destroy), Child.new
61
+ end
62
+
63
+ should "reject an association with a bad :dependent option" do
64
+ define_model :parent
65
+ define_model :child, :parent_id => :integer do
66
+ belongs_to :parent
67
+ end
68
+ assert_rejects @matcher.dependent(:destroy), Child.new
69
+ end
70
+ end
71
+
72
+ context "have_many" do
73
+ setup do
74
+ @matcher = have_many(:children)
75
+ end
76
+
77
+ should "accept a valid association without any options" do
78
+ define_model :child, :parent_id => :integer
79
+ define_model :parent do
80
+ has_many :children
81
+ end
82
+ assert_accepts @matcher, Parent.new
83
+ end
84
+
85
+ should "accept a valid association with a :through option" do
86
+ define_model :child
87
+ define_model :conception, :child_id => :integer,
88
+ :parent_id => :integer do
89
+ belongs_to :child
90
+ end
91
+ define_model :parent do
92
+ has_many :conceptions
93
+ has_many :children, :through => :conceptions
94
+ end
95
+ assert_accepts @matcher, Parent.new
96
+ end
97
+
98
+ should "accept a valid association with an :as option" do
99
+ define_model :child, :guardian_type => :string,
100
+ :guardian_id => :integer
101
+ define_model :parent do
102
+ has_many :children, :as => :guardian
103
+ end
104
+ assert_accepts @matcher, Parent.new
105
+ end
106
+
107
+ should "reject an association that has a nonexistent foreign key" do
108
+ define_model :child
109
+ define_model :parent do
110
+ has_many :children
111
+ end
112
+ assert_rejects @matcher, Parent.new
113
+ end
114
+
115
+ should "reject an association with a bad :as option" do
116
+ define_model :child, :caretaker_type => :string,
117
+ :caretaker_id => :integer
118
+ define_model :parent do
119
+ has_many :children, :as => :guardian
120
+ end
121
+ assert_rejects @matcher, Parent.new
122
+ end
123
+
124
+ should "reject an association that has a bad :through option" do
125
+ define_model :child, :parent_id => :integer
126
+ define_model :parent do
127
+ has_many :children
128
+ end
129
+ assert_rejects @matcher.through(:conceptions), Parent.new
130
+ end
131
+
132
+ should "reject an association that has the wrong :through option" do
133
+ define_model :child
134
+ define_model :conception, :child_id => :integer,
135
+ :parent_id => :integer do
136
+ belongs_to :child
137
+ end
138
+ define_model :parent do
139
+ has_many :conceptions
140
+ has_many :children, :through => :conceptions
141
+ end
142
+ assert_rejects @matcher.through(:relationships), Parent.new
143
+ end
144
+
145
+ should "accept an association with a valid :dependent option" do
146
+ define_model :child, :parent_id => :integer
147
+ define_model :parent do
148
+ has_many :children, :dependent => :destroy
149
+ end
150
+ assert_accepts @matcher.dependent(:destroy), Parent.new
151
+ end
152
+
153
+ should "reject an association with a bad :dependent option" do
154
+ define_model :child, :parent_id => :integer
155
+ define_model :parent do
156
+ has_many :children
157
+ end
158
+ assert_rejects @matcher.dependent(:destroy), Parent.new
159
+ end
160
+ end
161
+
162
+ context "have_one" do
163
+ setup do
164
+ @matcher = have_one(:profile)
165
+ end
166
+
167
+ should "accept a valid association without any options" do
168
+ define_model :profile, :person_id => :integer
169
+ define_model :person do
170
+ has_one :profile
171
+ end
172
+ assert_accepts @matcher, Person.new
173
+ end
174
+
175
+ should "accept a valid association with an :as option" do
176
+ define_model :profile, :profilable_id => :integer,
177
+ :profilable_type => :string
178
+ define_model :person do
179
+ has_one :profile, :as => :profilable
180
+ end
181
+ assert_accepts @matcher, Person.new
182
+ end
183
+
184
+ should "reject an association that has a nonexistent foreign key" do
185
+ define_model :profile
186
+ define_model :person do
187
+ has_one :profile
188
+ end
189
+ assert_rejects @matcher, Person.new
190
+ end
191
+
192
+ should "reject an association with a bad :as option" do
193
+ define_model :profile, :profilable_id => :integer,
194
+ :profilable_type => :string
195
+ define_model :person do
196
+ has_one :profile, :as => :describable
197
+ end
198
+ assert_rejects @matcher, Person.new
199
+ end
200
+
201
+ should "accept an association with a valid :dependent option" do
202
+ define_model :profile, :person_id => :integer
203
+ define_model :person do
204
+ has_one :profile, :dependent => :destroy
205
+ end
206
+ assert_accepts @matcher.dependent(:destroy), Person.new
207
+ end
208
+
209
+ should "reject an association with a bad :dependent option" do
210
+ define_model :profile, :person_id => :integer
211
+ define_model :person do
212
+ has_one :profile
213
+ end
214
+ assert_rejects @matcher.dependent(:destroy), Person.new
215
+ end
216
+ end
217
+
218
+ context "have_and_belong_to_many" do
219
+ setup do
220
+ @matcher = have_and_belong_to_many(:relatives)
221
+ end
222
+
223
+ should "accept a valid association" do
224
+ define_model :relatives
225
+ define_model :person do
226
+ has_and_belongs_to_many :relatives
227
+ end
228
+ define_model :people_relative, :person_id => :integer,
229
+ :relative_id => :integer
230
+ assert_accepts @matcher, Person.new
231
+ end
232
+
233
+ should "reject a nonexistent association" do
234
+ define_model :relatives
235
+ define_model :person
236
+ define_model :people_relative, :person_id => :integer,
237
+ :relative_id => :integer
238
+ assert_rejects @matcher, Person.new
239
+ end
240
+
241
+ should "reject an association with a nonexistent join table" do
242
+ define_model :relatives
243
+ define_model :person do
244
+ has_and_belongs_to_many :relatives
245
+ end
246
+ assert_rejects @matcher, Person.new
247
+ end
248
+
249
+ should "reject an association of the wrong type" do
250
+ define_model :relatives, :person_id => :integer
251
+ define_model :person do
252
+ has_many :relatives
253
+ end
254
+ assert_rejects @matcher, Person.new
255
+ end
256
+ end
257
+
258
+ end
@@ -0,0 +1,80 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class EnsureInclusionOfMatcherTest < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "an attribute which must be included in a range" do
6
+ setup do
7
+ @model = define_model(:example, :attr => :integer) do
8
+ validates_inclusion_of :attr, :in => 2..5
9
+ end.new
10
+ end
11
+
12
+ should "accept ensuring the correct range" do
13
+ assert_accepts ensure_inclusion_of(:attr).in_range(2..5), @model
14
+ end
15
+
16
+ should "reject ensuring a lower minimum value" do
17
+ assert_rejects ensure_inclusion_of(:attr).in_range(1..5), @model
18
+ end
19
+
20
+ should "reject ensuring a higher minimum value" do
21
+ assert_rejects ensure_inclusion_of(:attr).in_range(3..5), @model
22
+ end
23
+
24
+ should "reject ensuring a lower maximum value" do
25
+ assert_rejects ensure_inclusion_of(:attr).in_range(2..4), @model
26
+ end
27
+
28
+ should "reject ensuring a higher maximum value" do
29
+ assert_rejects ensure_inclusion_of(:attr).in_range(2..6), @model
30
+ end
31
+
32
+ should "not override the default message with a blank" do
33
+ assert_accepts ensure_inclusion_of(:attr).
34
+ in_range(2..5).
35
+ with_message(nil),
36
+ @model
37
+ end
38
+ end
39
+
40
+ context "an attribute with a custom ranged value validation" do
41
+ setup do
42
+ @model = define_model(:example, :attr => :string) do
43
+ validates_inclusion_of :attr, :in => 2..4, :message => 'not good'
44
+
45
+ end.new
46
+ end
47
+
48
+ should "accept ensuring the correct range" do
49
+ assert_accepts ensure_inclusion_of(:attr).
50
+ in_range(2..4).
51
+ with_message(/not good/),
52
+ @model
53
+ end
54
+ end
55
+
56
+ context "an attribute with custom range validations" do
57
+ setup do
58
+ define_model :example, :attr => :integer do
59
+ def validate
60
+ if attr < 2
61
+ errors.add(:attr, 'too low')
62
+ elsif attr > 5
63
+ errors.add(:attr, 'too high')
64
+ end
65
+ end
66
+ end
67
+ @model = Example.new
68
+ end
69
+
70
+ should "accept ensuring the correct range and messages" do
71
+ assert_accepts ensure_inclusion_of(:attr).
72
+ in_range(2..5).
73
+ with_low_message(/low/).
74
+ with_high_message(/high/),
75
+ @model
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,158 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class EnsureLengthOfMatcher < Test::Unit::TestCase # :nodoc:
4
+
5
+ context "an attribute with a non-zero minimum length validation" do
6
+ setup do
7
+ @model = define_model(:example, :attr => :string) do
8
+ validates_length_of :attr, :minimum => 4
9
+ end.new
10
+ end
11
+
12
+ should "accept ensuring the correct minimum length" do
13
+ assert_accepts ensure_length_of(:attr).is_at_least(4), @model
14
+ end
15
+
16
+ should "reject ensuring a lower minimum length with any message" do
17
+ assert_rejects ensure_length_of(:attr).
18
+ is_at_least(3).
19
+ with_short_message(/.*/),
20
+ @model
21
+ end
22
+
23
+ should "reject ensuring a higher minimum length with any message" do
24
+ assert_rejects ensure_length_of(:attr).
25
+ is_at_least(5).
26
+ with_short_message(/.*/),
27
+ @model
28
+ end
29
+
30
+ should "not override the default message with a blank" do
31
+ assert_accepts ensure_length_of(:attr).
32
+ is_at_least(4).
33
+ with_short_message(nil),
34
+ @model
35
+ end
36
+ end
37
+
38
+ context "an attribute with a minimum length validation of 0" do
39
+ setup do
40
+ @model = define_model(:example, :attr => :string) do
41
+ validates_length_of :attr, :minimum => 0
42
+ end.new
43
+ end
44
+
45
+ should "accept ensuring the correct minimum length" do
46
+ assert_accepts ensure_length_of(:attr).is_at_least(0), @model
47
+ end
48
+ end
49
+
50
+ context "an attribute with a maximum length" do
51
+ setup do
52
+ @model = define_model(:example, :attr => :string) do
53
+ validates_length_of :attr, :maximum => 4
54
+ end.new
55
+ end
56
+
57
+ should "accept ensuring the correct maximum length" do
58
+ assert_accepts ensure_length_of(:attr).is_at_most(4), @model
59
+ end
60
+
61
+ should "reject ensuring a lower maximum length with any message" do
62
+ assert_rejects ensure_length_of(:attr).
63
+ is_at_most(3).
64
+ with_long_message(/.*/),
65
+ @model
66
+ end
67
+
68
+ should "reject ensuring a higher maximum length with any message" do
69
+ assert_rejects ensure_length_of(:attr).
70
+ is_at_most(5).
71
+ with_long_message(/.*/),
72
+ @model
73
+ end
74
+
75
+ should "not override the default message with a blank" do
76
+ assert_accepts ensure_length_of(:attr).
77
+ is_at_most(4).
78
+ with_long_message(nil),
79
+ @model
80
+ end
81
+ end
82
+
83
+ context "an attribute with a required exact length" do
84
+ setup do
85
+ @model = define_model(:example, :attr => :string) do
86
+ validates_length_of :attr, :is => 4
87
+ end.new
88
+ end
89
+
90
+ should "accept ensuring the correct length" do
91
+ assert_accepts ensure_length_of(:attr).is_equal_to(4), @model
92
+ end
93
+
94
+ should "reject ensuring a lower maximum length with any message" do
95
+ assert_rejects ensure_length_of(:attr).
96
+ is_equal_to(3).
97
+ with_message(/.*/),
98
+ @model
99
+ end
100
+
101
+ should "reject ensuring a higher maximum length with any message" do
102
+ assert_rejects ensure_length_of(:attr).
103
+ is_equal_to(5).
104
+ with_message(/.*/),
105
+ @model
106
+ end
107
+
108
+ should "not override the default message with a blank" do
109
+ assert_accepts ensure_length_of(:attr).
110
+ is_equal_to(4).
111
+ with_message(nil),
112
+ @model
113
+ end
114
+ end
115
+
116
+ context "an attribute with a custom minimum length validation" do
117
+ setup do
118
+ @model = define_model(:example, :attr => :string) do
119
+ validates_length_of :attr, :minimum => 4, :too_short => 'short'
120
+ end.new
121
+ end
122
+
123
+ should "accept ensuring the correct minimum length" do
124
+ assert_accepts ensure_length_of(:attr).
125
+ is_at_least(4).
126
+ with_short_message(/short/),
127
+ @model
128
+ end
129
+
130
+ end
131
+
132
+ context "an attribute with a custom maximum length validation" do
133
+ setup do
134
+ @model = define_model(:example, :attr => :string) do
135
+ validates_length_of :attr, :maximum => 4, :too_long => 'long'
136
+ end.new
137
+ end
138
+
139
+ should "accept ensuring the correct minimum length" do
140
+ assert_accepts ensure_length_of(:attr).
141
+ is_at_most(4).
142
+ with_long_message(/long/),
143
+ @model
144
+ end
145
+
146
+ end
147
+
148
+ context "an attribute without a length validation" do
149
+ setup do
150
+ @model = define_model(:example, :attr => :string).new
151
+ end
152
+
153
+ should "reject ensuring a minimum length" do
154
+ assert_rejects ensure_length_of(:attr).is_at_least(1), @model
155
+ end
156
+ end
157
+
158
+ end