public
Rubygem
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/technicalpickles/shoulda.git
applied should_ensure_length_at_least patch via Chris O'Sullivan

git-svn-id: https://svn.thoughtbot.com/plugins/shoulda/trunk@447 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
tsaleh (author)
Sat Apr 12 14:21:27 -0700 2008
commit  ec9ead07ce32abbbd8bd49791fd9f5cbc4434853
tree    bba4735094bb6dc2755789befa165f97f4b34854
parent  452b49c5f7e37906f1e73ab462605021ad123d97
...
220
221
222
223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
225
226
...
220
221
222
 
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
0
@@ -220,7 +220,41 @@ module ThoughtBot # :nodoc:
0
             assert_does_not_contain(object.errors.on(attribute), long_message, "when set to \"#{max_value}\"")
0
           end
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
+ short_message = get_options!([opts], :short_message)
0
+ short_message ||= /short/
0
+
0
+ klass = model_class
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.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), 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.send("#{attribute}=", valid_value)
0
+ assert object.save, "Could not save #{klass} with #{attribute} set to \"#{valid_value}\""
0
+ end
0
+ end
0
 
0
       # Ensure that the attribute is in the range specified
0
       # Requires an existing record
...
1
2
3
 
 
4
...
1
2
3
4
5
6
0
@@ -1,4 +1,6 @@
0
 class Tag < ActiveRecord::Base
0
   has_many :taggings
0
   has_many :posts, :through => :taggings
0
+
0
+ validates_length_of :name, :minimum => 2
0
 end
...
5
6
7
 
 
8
...
5
6
7
8
9
10
0
@@ -5,4 +5,6 @@ class TagTest < Test::Unit::TestCase
0
 
0
   should_have_many :taggings
0
   should_have_many :posts
0
+
0
+ should_ensure_length_at_least :name, 2
0
 end

Comments

    No one has commented yet.