Sha256: c3b8a785ffd66f23530ec308aa0b07b3d43dce3f2bcc24ccae6cf9732694bfc0

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8
require 'cases/helper'
require 'cases/tests_database'

require 'models/topic'
require 'models/developer'
require 'models/person'
require 'models/custom_reader'

class PresenceValidationTest < ActiveModel::TestCase
  include ActiveModel::TestsDatabase
  include ActiveModel::ValidationsRepairHelper

  repair_validations(Topic)

  def test_validate_presences
    Topic.validates_presence_of(:title, :content)

    t = Topic.create
    assert !t.save
    assert_equal ["can't be blank"], t.errors[:title]
    assert_equal ["can't be blank"], t.errors[:content]

    t.title = "something"
    t.content  = "   "

    assert !t.save
    assert_equal ["can't be blank"], t.errors[:content]

    t.content = "like stuff"

    assert t.save
  end

  # def test_validates_presence_of_with_custom_message_using_quotes
  #   repair_validations(Developer) do
  #     Developer.validates_presence_of :non_existent, :message=> "This string contains 'single' and \"double\" quotes"
  #     d = Developer.new
  #     d.name = "Joe"
  #     assert !d.valid?
  #     assert_equal ["This string contains 'single' and \"double\" quotes"], d.errors[:non_existent]
  #   end
  # end

  def test_validates_presence_of_for_ruby_class
    repair_validations(Person) do
      Person.validates_presence_of :karma

      p = Person.new
      assert p.invalid?

      assert_equal ["can't be blank"], p.errors[:karma]

      p.karma = "Cold"
      assert p.valid?
    end
  end
  
  def test_validates_presence_of_for_ruby_class_with_custom_reader
    repair_validations(Person) do
      CustomReader.validates_presence_of :karma

      p = CustomReader.new
      assert p.invalid?

      assert_equal ["can't be blank"], p.errors[:karma]

      p[:karma] = "Cold"
      assert p.valid?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
recliner-0.0.1 vendor/activemodel/test/cases/validations/presence_validation_test.rb