mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
Merge branch '32-please-add-should_ensure_length_is' of git://github.com/thechrisoshow/shoulda into pull-requests
* '32-please-add-should_ensure_length_is' of git://github.com/thechrisoshow/shoulda: Added should_ensure_length_is (including tests) [#32]
This commit is contained in:
commit
0c876844f8
6 changed files with 60 additions and 1 deletions
|
@ -271,6 +271,50 @@ module ThoughtBot # :nodoc:
|
|||
assert object.save, "Could not save #{klass} with #{attribute} set to \"#{valid_value}\""
|
||||
end
|
||||
end
|
||||
|
||||
# Ensures that the length of the attribute is exactly a certain length
|
||||
# Requires an existing record
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/short/</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_ensure_length_is :ssn, 9
|
||||
#
|
||||
def should_ensure_length_is(attribute, length, opts = {})
|
||||
message = get_options!([opts], :message)
|
||||
message ||= /wrong length/
|
||||
|
||||
klass = model_class
|
||||
|
||||
should "not allow #{attribute} to be less than #{length} chars long" do
|
||||
min_value = "x" * (length - 1)
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send("#{attribute}=", min_value)
|
||||
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{min_value}\""
|
||||
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{min_value}\""
|
||||
assert_contains(object.errors.on(attribute), message, "when set to \"#{min_value}\"")
|
||||
end
|
||||
|
||||
should "not allow #{attribute} to be greater than #{length} chars long" do
|
||||
max_value = "x" * (length + 1)
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send("#{attribute}=", max_value)
|
||||
assert !object.save, "Saved #{klass} with #{attribute} set to \"#{max_value}\""
|
||||
assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{max_value}\""
|
||||
assert_contains(object.errors.on(attribute), message, "when set to \"#{max_value}\"")
|
||||
end
|
||||
|
||||
should "allow #{attribute} to be #{length} chars long" do
|
||||
valid_value = "x" * (length)
|
||||
assert object = klass.find(:first), "Can't find first #{klass}"
|
||||
object.send("#{attribute}=", valid_value)
|
||||
object.save
|
||||
assert_does_not_contain(object.errors.on(attribute), message, "when set to \"#{valid_value}\"")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# Ensure that the attribute is in the range specified
|
||||
# Requires an existing record
|
||||
|
|
1
test/fixtures/users.yml
vendored
1
test/fixtures/users.yml
vendored
|
@ -3,3 +3,4 @@ first:
|
|||
name: Some dude
|
||||
age: 2
|
||||
email: none@none.com
|
||||
ssn: 123456789
|
||||
|
|
|
@ -22,7 +22,7 @@ class UsersControllerTest < Test::Unit::TestCase
|
|||
resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
|
||||
resource.formats = [:html, :xml]
|
||||
|
||||
resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13}
|
||||
resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13, :ssn => "123456789"}
|
||||
resource.update.params = { :name => "sue" }
|
||||
|
||||
resource.create.redirect = "user_url(@user)"
|
||||
|
|
|
@ -11,4 +11,6 @@ class User < ActiveRecord::Base
|
|||
validates_inclusion_of :age, :in => 1..100
|
||||
validates_acceptance_of :eula
|
||||
validates_uniqueness_of :email, :scope => :name
|
||||
validates_length_of :ssn, :is => 9, :message => "Social Security Number is not the right length"
|
||||
validates_numericality_of :ssn
|
||||
end
|
||||
|
|
9
test/rails_root/db/migrate/009_add_ssn_to_users.rb
Normal file
9
test/rails_root/db/migrate/009_add_ssn_to_users.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class AddSsnToUsers < ActiveRecord::Migration
|
||||
def self.up
|
||||
add_column :users, :ssn, :string
|
||||
end
|
||||
|
||||
def self.down
|
||||
remove_column :users, :ssn
|
||||
end
|
||||
end
|
|
@ -24,4 +24,7 @@ class UserTest < Test::Unit::TestCase
|
|||
:null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
|
||||
should_require_acceptance_of :eula
|
||||
should_require_unique_attributes :email, :scoped_to => :name
|
||||
|
||||
should_ensure_length_is :ssn, 9, :message => "Social Security Number is not the right length"
|
||||
should_only_allow_numeric_values_for :ssn
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue