0
+class Test::Unit::TestCase
0
+ # Ensures that the model cannot be saved if one of the attributes listed is not present.
0
+ # Requires an existing record
0
+ def should_require_attributes(*attributes)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ attributes.each do |attribute|
0
+ should "require #{attribute} to be set" do
0
+ assert !object.valid?, "Instance is still valid"
0
+ assert object.errors.on(attribute), "No errors found"
0
+ assert object.errors.on(attribute).to_a.include?("can't be blank"), "Error message doesn't match"
0
+ # Ensures that the model cannot be saved if one of the attributes listed is not unique.
0
+ # Requires an existing record
0
+ def should_require_unique_attributes(*attributes)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ attributes.each do |attribute|
0
+ attribute = attribute.to_sym
0
+ should "require unique value for #{attribute}" do
0
+ assert existing = klass.find(:first), "Can't find first #{klass}"
0
+ object.send(:"#{attribute}=", existing.send(attribute))
0
+ assert !object.valid?, "Instance is still valid"
0
+ assert object.errors.on(attribute), "No errors found"
0
+ assert object.errors.on(attribute).to_a.include?('has already been taken'), "Error message doesn't match"
0
+ # Ensures that the attribute cannot be set on update
0
+ # Requires an existing record
0
+ def should_protect_attributes(*attributes)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ attributes.each do |attribute|
0
+ attribute = attribute.to_sym
0
+ should "not allow #{attribute} to be changed by update" do
0
+ assert object = klass.find(:first), "Can't find first #{klass}"
0
+ value = object[attribute]
0
+ assert object.update_attributes({ attribute => 1 }),
0
+ "Cannot update #{klass} with { :#{attribute} => 1 }, #{object.errors.full_messages.to_sentence}"
0
+ assert object.valid?, "#{klass} isn't valid after changing #{attribute}"
0
+ assert_equal value, object[attribute], "Was able to change #{klass}##{attribute}"
0
+ # Ensures that the attribute cannot be set to the given values
0
+ # Requires an existing record
0
+ def should_not_allow_values_for(attribute, *bad_values)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ bad_values.each do |v|
0
+ should "not allow #{attribute} to be set to \"#{v}\"" do
0
+ assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object.send("#{attribute}=", v)
0
+ assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
0
+ assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
0
+ assert_match(/invalid/, object.errors.on(attribute), "Error set on #{attribute} doesn't include \"invalid\" when set to \"#{v}\"")
0
+ # Ensures that the attribute can be set to the given values.
0
+ # Requires an existing record
0
+ def should_allow_values_for(attribute, *good_values)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ good_values.each do |v|
0
+ should "allow #{attribute} to be set to \"#{v}\"" do
0
+ assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object.send("#{attribute}=", v)
0
+ # assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
0
+ assert_no_match(/invalid/, object.errors.on(attribute), "Error set on #{attribute} includes \"invalid\" when set to \"#{v}\"")
0
+ # Ensures that the length of the attribute is in the given range
0
+ # Requires an existing record
0
+ def should_ensure_length_in_range(attribute, range)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ min_length = range.first
0
+ max_length = range.last
0
+ min_value = "x" * (min_length - 1)
0
+ max_value = "x" * (max_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_match(/short/, object.errors.on(attribute), "Error set on #{attribute} doesn't include \"short\" when set to \"#{min_value}\"")
0
+ should "not allow #{attribute} to be more than #{max_length} chars long" do
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_match(/long/, object.errors.on(attribute), "Error set on #{attribute} doesn't include \"long\" when set to \"#{max_value}\"")
0
+ # Ensure that the attribute is in the range specified
0
+ # Requires an existing record
0
+ def should_ensure_value_in_range(attribute, range)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ should "not allow #{attribute} to be less than #{min}" do
0
+ assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object.send("#{attribute}=", v)
0
+ assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
0
+ assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
0
+ should "not allow #{attribute} to be more than #{max}" do
0
+ assert object = klass.find(:first), "Can't find first #{klass}"
0
+ object.send("#{attribute}=", v)
0
+ assert !object.save, "Saved #{klass} with #{attribute} set to \"#{v}\""
0
+ assert object.errors.on(attribute), "There are no errors set on #{attribute} after being set to \"#{v}\""
0
+ # Ensure that the attribute is numeric
0
+ # Requires an existing record
0
+ def should_only_allow_numeric_values_for(*attributes)
0
+ klass = self.name.gsub(/Test$/, '').constantize
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
+ assert object.errors.on(attribute), "No errors found"
0
+ assert object.errors.on(attribute).to_a.include?('is not a number'), "Error message doesn't match"
0
+ # Ensures that the has_many relationship exists.
0
+ # The last parameter may be a hash of options. Currently, the only supported option
0
+ def should_have_many(*associations)
0
+ opts = associations.last.is_a?(Hash) ? associations.pop : {}
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ associations.each do |association|
0
+ should "have many #{association}#{" through #{opts[:through]}" if opts[:through]}" do
0
+ reflection = klass.reflect_on_association(association)
0
+ assert_equal :has_many, reflection.macro
0
+ assert_equal(opts[:through], reflection.options[:through]) if opts[:through]
0
+ # Ensures that the has_and_belongs_to_many relationship exists.
0
+ def should_have_and_belong_to_many(*associations)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ associations.each do |association|
0
+ should "should have and belong to many #{association}" do
0
+ assert klass.reflect_on_association(association)
0
+ assert_equal :has_and_belongs_to_many, klass.reflect_on_association(association).macro
0
+ # Ensure that the has_one relationship exists.
0
+ def should_have_one(*associations)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ associations.each do |association|
0
+ should "have one #{association}" do
0
+ assert klass.reflect_on_association(association)
0
+ assert_equal :has_one, klass.reflect_on_association(association).macro
0
+ # Ensure that the belongs_to relationship exists.
0
+ def should_belong_to(*associations)
0
+ klass = self.name.gsub(/Test$/, '').constantize
0
+ associations.each do |association|
0
+ should "belong_to #{association}" do
0
+ assert klass.reflect_on_association(association)
0
+ assert_equal :belongs_to, klass.reflect_on_association(association).macro
0
\ No newline at end of file
Comments
No one has commented yet.