class AbsenceValidationTest

Public Class Methods

name() click to toggle source
# File activerecord/test/cases/validations/absence_validation_test.rb, line 12
def self.name; "Boy" end

Public Instance Methods

test_does_not_call_to_a_on_associations() click to toggle source
# File activerecord/test/cases/validations/absence_validation_test.rb, line 50
def test_does_not_call_to_a_on_associations
  boy_klass = Class.new(Man) do
    def self.name; "Boy" end
    validates_absence_of :face
  end

  face_with_to_a = Face.new
  def face_with_to_a.to_a; ["(/)", '(\)']; end

  assert_nothing_raised { boy_klass.new(face: face_with_to_a).valid? }
end
test_has_many_marked_for_destruction() click to toggle source
# File activerecord/test/cases/validations/absence_validation_test.rb, line 34
def test_has_many_marked_for_destruction
  boy_klass = Class.new(Man) do
    def self.name; "Boy" end
    validates_absence_of :interests
  end
  boy = boy_klass.new
  boy.interests << [i1 = Interest.new, i2 = Interest.new]
  assert_not boy.valid?, "should not be valid if has_many association is present"

  i1.mark_for_destruction
  assert_not boy.valid?, "should not be valid if has_many association is present"

  i2.mark_for_destruction
  assert boy.valid?
end
test_has_one_marked_for_destruction() click to toggle source
# File activerecord/test/cases/validations/absence_validation_test.rb, line 20
def test_has_one_marked_for_destruction
  boy_klass = Class.new(Man) do
    def self.name; "Boy" end
    validates_absence_of :face
  end

  boy = boy_klass.new(face: Face.new)
  assert_not boy.valid?, "should not be valid if has_one association is present"
  assert_equal 1, boy.errors[:face].size, "should only add one error"

  boy.face.mark_for_destruction
  assert boy.valid?, "should be valid if association is marked for destruction"
end
test_non_association() click to toggle source
# File activerecord/test/cases/validations/absence_validation_test.rb, line 10
def test_non_association
  boy_klass = Class.new(Man) do
    def self.name; "Boy" end
    validates_absence_of :name
  end

  assert boy_klass.new.valid?
  assert_not boy_klass.new(name: "Alex").valid?
end
test_validates_absence_of() click to toggle source
# File activemodel/test/cases/validations/absence_validation_test.rb, line 15
def test_validates_absence_of
  Topic.validates_absence_of(:title, :content)
  t = Topic.new
  t.title = "foo"
  t.content = "bar"
  assert t.invalid?
  assert_equal ["must be blank"], t.errors[:title]
  assert_equal ["must be blank"], t.errors[:content]
  t.title = ""
  t.content = "something"
  assert t.invalid?
  assert_equal ["must be blank"], t.errors[:content]
  assert_equal [], t.errors[:title]
  t.content = ""
  assert t.valid?
end
test_validates_absence_of_for_ruby_class() click to toggle source
# File activemodel/test/cases/validations/absence_validation_test.rb, line 50
def test_validates_absence_of_for_ruby_class
  Person.validates_absence_of :karma
  p = Person.new
  p.karma = "good"
  assert p.invalid?
  assert_equal ["must be blank"], p.errors[:karma]
  p.karma = nil
  assert p.valid?
end
test_validates_absence_of_for_ruby_class_with_custom_reader() click to toggle source
# File activemodel/test/cases/validations/absence_validation_test.rb, line 60
def test_validates_absence_of_for_ruby_class_with_custom_reader
  CustomReader.validates_absence_of :karma
  p = CustomReader.new
  p[:karma] = "excellent"
  assert p.invalid?
  assert_equal ["must be blank"], p.errors[:karma]
  p[:karma] = ""
  assert p.valid?
end
test_validates_absence_of_virtual_attribute_on_model() click to toggle source
# File activerecord/test/cases/validations/absence_validation_test.rb, line 62
def test_validates_absence_of_virtual_attribute_on_model
  repair_validations(Interest) do
    Interest.send(:attr_accessor, :token)
    Interest.validates_absence_of(:token)

    interest = Interest.create!(topic: "Thought Leadering")
    assert interest.valid?

    interest.token = "tl"

    assert interest.invalid?
  end
end
test_validates_absence_of_with_array_arguments() click to toggle source
# File activemodel/test/cases/validations/absence_validation_test.rb, line 32
def test_validates_absence_of_with_array_arguments
  Topic.validates_absence_of %w(title content)
  t = Topic.new
  t.title = "foo"
  t.content = "bar"
  assert t.invalid?
  assert_equal ["must be blank"], t.errors[:title]
  assert_equal ["must be blank"], t.errors[:content]
end
test_validates_absence_of_with_custom_error_using_quotes() click to toggle source
# File activemodel/test/cases/validations/absence_validation_test.rb, line 42
def test_validates_absence_of_with_custom_error_using_quotes
  Person.validates_absence_of :karma, message: "This string contains 'single' and \"double\" quotes"
  p = Person.new
  p.karma = "good"
  assert p.invalid?
  assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last
end