0
@@ -271,6 +271,50 @@ module ThoughtBot # :nodoc:
0
assert object.save, "Could not save #{klass} with #{attribute} set to \"#{valid_value}\""
0
+ # Ensures that the length of the attribute is exactly a certain length
0
+ # Requires an existing record
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
+ # should_ensure_length_is :ssn, 9
0
+ def should_ensure_length_is(attribute, length, opts = {})
0
+ message = get_options!([opts], :message)
0
+ message ||= /wrong length/
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
+ 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
+ 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
+ assert_does_not_contain(object.errors.on(attribute), message, "when set to \"#{valid_value}\"")
0
# Ensure that the attribute is in the range specified
0
# Requires an existing record
Comments
No one has commented yet.