public
Fork of thoughtbot/shoulda
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/snowblink/shoulda.git
Added should_ensure_length_is (including tests) [#32]
thechrisoshow (author)
Thu Jun 19 09:23:45 -0700 2008
commit  6511bcf398b47c6dff17d87f963dda01ff27051b
tree    182ca19cde5f3d86754061cfde7e1d5b214b421d
parent  5e4222694b8373c2a6f56afd9cef99c9cb223aa9
...
271
272
273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
275
276
...
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
0
@@ -271,6 +271,50 @@ module ThoughtBot # :nodoc:
0
           assert object.save, "Could not save #{klass} with #{attribute} set to \"#{valid_value}\""
0
         end
0
       end
0
+
0
+ # Ensures that the length of the attribute is exactly a certain length
0
+ # Requires an existing record
0
+ #
0
+ # Options:
0
+ # * <tt>: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_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
 
0
       # Ensure that the attribute is in the range specified
0
       # Requires an existing record
...
3
4
5
 
...
3
4
5
6
0
@@ -3,3 +3,4 @@ first:
0
   name: Some dude
0
   age: 2
0
   email: none@none.com
0
+ ssn: 123456789
...
22
23
24
25
 
26
27
28
...
22
23
24
 
25
26
27
28
0
@@ -22,7 +22,7 @@ class UsersControllerTest < Test::Unit::TestCase
0
     resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
0
     resource.formats = [:html, :xml]
0
     
0
- resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
0
+ resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13, :ssn => "123456789"}
0
     resource.update.params = { :name => "sue" }
0
     
0
     resource.create.redirect = "user_url(@user)"
...
11
12
13
 
 
14
...
11
12
13
14
15
16
0
@@ -11,4 +11,6 @@ class User < ActiveRecord::Base
0
   validates_inclusion_of :age, :in => 1..100
0
   validates_acceptance_of :eula
0
   validates_uniqueness_of :email, :scope => :name
0
+ validates_length_of :ssn, :is => 9, :message => "Social Security Number is not the right length"
0
+ validates_numericality_of :ssn
0
 end
...
24
25
26
 
 
 
27
...
24
25
26
27
28
29
30
0
@@ -24,4 +24,7 @@ class UserTest < Test::Unit::TestCase
0
                                 :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
0
   should_require_acceptance_of :eula
0
   should_require_unique_attributes :email, :scoped_to => :name
0
+
0
+ should_ensure_length_is :ssn, 9, :message => "Social Security Number is not the right length"
0
+ should_only_allow_numeric_values_for :ssn
0
 end

Comments

    No one has commented yet.