public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
Removed unnecessary database access for several active record macro 
helpers
rmm5t (author)
Fri Jun 27 20:14:02 -0700 2008
commit  13f5c2ae8228826f4d12f24a53ecf257659c9c67
tree    cded96279efc22bb720afa261af0d9f75639e139
parent  9f8c29edf3e632cdbc9756f6c5792bdc8cc9494c
...
71
72
73
74
 
75
76
77
78
 
79
80
81
...
92
93
94
95
 
96
97
98
...
167
168
169
170
 
171
172
 
173
174
175
...
187
188
189
190
 
191
192
 
193
194
195
...
220
221
222
223
 
224
225
 
226
227
228
...
232
233
234
235
 
236
237
 
238
239
240
241
242
243
244
 
245
246
 
247
248
249
...
252
253
254
255
 
256
257
 
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
 
 
 
 
 
 
 
 
 
 
 
274
275
276
 
277
278
 
279
280
281
282
 
283
284
 
285
286
287
288
289
290
291
 
292
293
 
 
294
295
296
...
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
341
342
...
361
362
363
364
 
365
366
 
367
368
369
370
371
372
373
 
374
375
 
376
377
378
379
380
381
 
382
383
 
384
385
386
387
388
389
390
 
391
392
 
393
394
395
...
411
412
413
414
415
416
 
 
 
417
418
419
...
71
72
73
 
74
75
76
77
 
78
79
80
81
...
92
93
94
 
95
96
97
98
...
167
168
169
 
170
171
 
172
173
174
175
...
187
188
189
 
190
191
 
192
193
194
195
...
220
221
222
 
223
224
 
225
226
227
228
...
232
233
234
 
235
236
 
237
238
239
240
241
242
243
 
244
245
 
246
247
248
249
...
252
253
254
 
255
256
 
257
258
259
260
261
262
 
 
 
 
 
 
 
 
 
 
 
263
264
265
266
267
268
269
270
271
272
273
274
275
 
276
277
 
278
279
280
281
 
282
283
 
284
285
286
287
288
289
290
 
291
292
 
293
294
295
296
297
...
306
307
308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
...
361
362
363
 
364
365
 
366
367
368
369
370
371
372
 
373
374
 
375
376
377
378
379
380
 
381
382
 
383
384
385
386
387
388
389
 
390
391
 
392
393
394
395
...
411
412
413
 
 
 
414
415
416
417
418
419
0
@@ -71,11 +71,11 @@ module ThoughtBot # :nodoc:
0
             assert existing = klass.find(:first), "Can't find first #{klass}"
0
             object = klass.new
0
             
0
- object.send(:"#{attribute}=", existing.send(attribute))
0
+ object.send("#{attribute}=", existing.send(attribute))
0
             if !scope.blank?
0
               scope.each do |s|
0
                 assert_respond_to object, :"#{s}=", "#{klass.name} doesn't seem to have a #{s} attribute."
0
- object.send(:"#{s}=", existing.send(s))
0
+ object.send("#{s}=", existing.send(s))
0
               end
0
             end
0
             
0
@@ -92,7 +92,7 @@ module ThoughtBot # :nodoc:
0
             if !scope.blank?
0
               scope.each do |s|
0
                 # Assume the scope is a foreign key if the field is nil
0
- object.send(:"#{s}=", existing.send(s).nil? ? 1 : existing.send(s).next)
0
+ object.send("#{s}=", existing.send(s).nil? ? 1 : existing.send(s).next)
0
               end
0
 
0
               object.errors.clear
0
@@ -167,9 +167,9 @@ module ThoughtBot # :nodoc:
0
         klass = model_class
0
         bad_values.each do |v|
0
           should "not allow #{attribute} to be set to #{v.inspect}" do
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
             object.send("#{attribute}=", v)
0
- assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
0
+ assert !object.valid?, "#{klass.name} allowed \"#{v}\" as a value for #{attribute}"
0
             assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
0
             assert_contains(object.errors.on(attribute), message, "when set to \"#{v}\"")
0
           end
0
@@ -187,9 +187,9 @@ module ThoughtBot # :nodoc:
0
         klass = model_class
0
         good_values.each do |v|
0
           should "allow #{attribute} to be set to #{v.inspect}" do
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
             object.send("#{attribute}=", v)
0
- object.save
0
+ object.valid?
0
             assert_nil object.errors.on(attribute)
0
           end
0
         end
0
@@ -220,9 +220,9 @@ module ThoughtBot # :nodoc:
0
         if min_length > 0
0
           should "not allow #{attribute} to be less than #{min_length} chars long" do
0
             min_value = "x" * (min_length - 1)
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
             object.send("#{attribute}=", min_value)
0
- assert !object.save, "Saved #{klass} with #{attribute} set to \"#{min_value}\""
0
+ assert !object.valid?, "#{klass.name} allowed \"#{min_value}\" as a value for #{attribute}"
0
             assert object.errors.on(attribute),
0
                    "There are no errors set on #{attribute} after being set to \"#{min_value}\""
0
             assert_contains(object.errors.on(attribute), short_message, "when set to \"#{min_value}\"")
0
@@ -232,18 +232,18 @@ module ThoughtBot # :nodoc:
0
         if min_length > 0
0
           should "allow #{attribute} to be exactly #{min_length} chars long" do
0
             min_value = "x" * min_length
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
             object.send("#{attribute}=", min_value)
0
- object.save
0
+ object.valid?
0
             assert_does_not_contain(object.errors.on(attribute), short_message, "when set to \"#{min_value}\"")
0
           end
0
         end
0
     
0
         should "not allow #{attribute} to be more than #{max_length} chars long" do
0
           max_value = "x" * (max_length + 1)
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
           object.send("#{attribute}=", max_value)
0
- assert !object.save, "Saved #{klass} with #{attribute} set to \"#{max_value}\""
0
+ assert !object.valid?, "#{klass.name} allowed \"#{max_value}\" as a value for #{attribute}"
0
           assert object.errors.on(attribute),
0
                  "There are no errors set on #{attribute} after being set to \"#{max_value}\""
0
           assert_contains(object.errors.on(attribute), long_message, "when set to \"#{max_value}\"")
0
@@ -252,45 +252,46 @@ module ThoughtBot # :nodoc:
0
         unless same_length
0
           should "allow #{attribute} to be exactly #{max_length} chars long" do
0
             max_value = "x" * max_length
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
             object.send("#{attribute}=", max_value)
0
- object.save
0
+ object.valid?
0
             assert_does_not_contain(object.errors.on(attribute), long_message, "when set to \"#{max_value}\"")
0
           end
0
         end
0
       end
0
       
0
- # Ensures that the length of the attribute is at least a certain length
0
- # Requires an existing record
0
- #
0
- # Options:
0
- # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
- # Regexp or string. Default = <tt>/short/</tt>
0
- #
0
- # Example:
0
- # should_ensure_length_at_least :name, 3
0
- #
0
- def should_ensure_length_at_least(attribute, min_length, opts = {})
0
+ # Ensures that the length of the attribute is at least a certain length
0
+ # Requires an existing record
0
+ #
0
+ # Options:
0
+ # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
0
+ # Regexp or string. Default = <tt>/short/</tt>
0
+ #
0
+ # Example:
0
+ # should_ensure_length_at_least :name, 3
0
+ #
0
+ def should_ensure_length_at_least(attribute, min_length, opts = {})
0
         short_message = get_options!([opts], :short_message)
0
         short_message ||= /short/
0
-
0
+
0
         klass = model_class
0
-
0
+
0
         if min_length > 0
0
           min_value = "x" * (min_length - 1)
0
           should "not allow #{attribute} to be less than #{min_length} chars long" do
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
             object.send("#{attribute}=", min_value)
0
- assert !object.save, "Saved #{klass} with #{attribute} set to \"#{min_value}\""
0
+ assert !object.valid?, "#{klass} allowed \"#{min_value}\" as a value for #{attribute}"
0
             assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{min_value}\""
0
             assert_contains(object.errors.on(attribute), short_message, "when set to \"#{min_value}\"")
0
           end
0
         end
0
         should "allow #{attribute} to be at least #{min_length} chars long" do
0
           valid_value = "x" * (min_length)
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
           object.send("#{attribute}=", valid_value)
0
- assert object.save, "Could not save #{klass} with #{attribute} set to \"#{valid_value}\""
0
+ object.valid?
0
+ assert_does_not_contain(object.errors.on(attribute), short_message, "when set to \"#{valid_value}\"")
0
         end
0
       end
0
       
0
@@ -305,38 +306,37 @@ module ThoughtBot # :nodoc:
0
       # should_ensure_length_is :ssn, 9
0
       #
0
       def should_ensure_length_is(attribute, length, opts = {})
0
- message = get_options!([opts], :message)
0
- message ||= /wrong length/
0
-
0
- klass = model_class
0
-
0
- should "not allow #{attribute} to be less than #{length} chars long" do
0
- min_value = "x" * (length - 1)
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
- object.send("#{attribute}=", min_value)
0
- assert !object.save, "Saved #{klass} with #{attribute} set to \"#{min_value}\""
0
- assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{min_value}\""
0
- assert_contains(object.errors.on(attribute), message, "when set to \"#{min_value}\"")
0
- end
0
-
0
- should "not allow #{attribute} to be greater than #{length} chars long" do
0
- max_value = "x" * (length + 1)
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
- object.send("#{attribute}=", max_value)
0
- assert !object.save, "Saved #{klass} with #{attribute} set to \"#{max_value}\""
0
- assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{max_value}\""
0
- assert_contains(object.errors.on(attribute), message, "when set to \"#{max_value}\"")
0
- end
0
-
0
- should "allow #{attribute} to be #{length} chars long" do
0
- valid_value = "x" * (length)
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
- object.send("#{attribute}=", valid_value)
0
- object.save
0
- assert_does_not_contain(object.errors.on(attribute), message, "when set to \"#{valid_value}\"")
0
- end
0
-
0
- end
0
+ message = get_options!([opts], :message)
0
+ message ||= /wrong length/
0
+
0
+ klass = model_class
0
+
0
+ should "not allow #{attribute} to be less than #{length} chars long" do
0
+ min_value = "x" * (length - 1)
0
+ object = klass.new
0
+ object.send("#{attribute}=", min_value)
0
+ assert !object.valid?, "#{klass} allowed \"#{min_value}\" as a value for #{attribute}"
0
+ assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{min_value}\""
0
+ assert_contains(object.errors.on(attribute), message, "when set to \"#{min_value}\"")
0
+ end
0
+
0
+ should "not allow #{attribute} to be greater than #{length} chars long" do
0
+ max_value = "x" * (length + 1)
0
+ object = klass.new
0
+ object.send("#{attribute}=", max_value)
0
+ assert !object.valid?, "#{klass} allowed \"#{max_value}\" as a value for #{attribute}"
0
+ assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{max_value}\""
0
+ assert_contains(object.errors.on(attribute), message, "when set to \"#{max_value}\"")
0
+ end
0
+
0
+ should "allow #{attribute} to be #{length} chars long" do
0
+ valid_value = "x" * (length)
0
+ object = klass.new
0
+ object.send("#{attribute}=", valid_value)
0
+ object.valid?
0
+ assert_does_not_contain(object.errors.on(attribute), message, "when set to \"#{valid_value}\"")
0
+ end
0
+ end
0
 
0
       # Ensure that the attribute is in the range specified
0
       # Requires an existing record
0
@@ -361,35 +361,35 @@ module ThoughtBot # :nodoc:
0
 
0
         should "not allow #{attribute} to be less than #{min}" do
0
           v = min - 1
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
           object.send("#{attribute}=", v)
0
- assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
0
+ assert !object.valid?, "#{klass} allowed \"#{v}\" as a value for #{attribute}"
0
           assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
0
           assert_contains(object.errors.on(attribute), low_message, "when set to \"#{v}\"")
0
         end
0
 
0
         should "allow #{attribute} to be #{min}" do
0
           v = min
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
           object.send("#{attribute}=", v)
0
- object.save
0
+ object.valid?
0
           assert_does_not_contain(object.errors.on(attribute), low_message, "when set to \"#{v}\"")
0
         end
0
 
0
         should "not allow #{attribute} to be more than #{max}" do
0
           v = max + 1
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
           object.send("#{attribute}=", v)
0
- assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
0
+ assert !object.valid?, "#{klass} allowed \"#{v}\" as a value for #{attribute}"
0
           assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
0
           assert_contains(object.errors.on(attribute), high_message, "when set to \"#{v}\"")
0
         end
0
 
0
         should "allow #{attribute} to be #{max}" do
0
           v = max
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object = klass.new
0
           object.send("#{attribute}=", v)
0
- object.save
0
+ object.valid?
0
           assert_does_not_contain(object.errors.on(attribute), high_message, "when set to \"#{v}\"")
0
         end
0
       end
0
@@ -411,9 +411,9 @@ module ThoughtBot # :nodoc:
0
         attributes.each do |attribute|
0
           attribute = attribute.to_sym
0
           should "only allow numeric values for #{attribute}" do
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
- object.send(:"#{attribute}=", "abcd")
0
- assert !object.valid?, "Instance is still valid"
0
+ object = klass.new
0
+ object.send("#{attribute}=", "abcd")
0
+ assert !object.valid?, "#{klass} allowed \"abcd\" as a value for #{attribute}"
0
             assert_contains(object.errors.on(attribute), message)
0
           end
0
         end

Comments

    No one has commented yet.