2009-03-20 11:07:49 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'cases/helper'
|
2009-03-20 12:02:12 -04:00
|
|
|
require 'cases/tests_database'
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
require 'models/topic'
|
2009-03-20 18:21:27 -04:00
|
|
|
require 'models/developer'
|
|
|
|
require 'models/person'
|
2009-09-05 19:54:19 -04:00
|
|
|
require 'models/custom_reader'
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
class PresenceValidationTest < ActiveModel::TestCase
|
2009-03-20 12:02:12 -04:00
|
|
|
include ActiveModel::TestsDatabase
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-02-25 16:07:48 -05:00
|
|
|
teardown do
|
|
|
|
Topic.reset_callbacks(:validate)
|
|
|
|
Person.reset_callbacks(:validate)
|
|
|
|
CustomReader.reset_callbacks(:validate)
|
|
|
|
end
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
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
|
2010-02-25 16:07:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'accepts array arguments' do
|
|
|
|
Topic.validates_presence_of %w(title content)
|
|
|
|
t = Topic.new
|
|
|
|
assert !t.valid?
|
|
|
|
assert_equal ["can't be blank"], t.errors[:title]
|
|
|
|
assert_equal ["can't be blank"], t.errors[:content]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
def test_validates_acceptance_of_with_custom_error_using_quotes
|
2010-02-25 16:07:48 -05:00
|
|
|
Person.validates_presence_of :karma, :message => "This string contains 'single' and \"double\" quotes"
|
2009-12-23 07:30:58 -05:00
|
|
|
p = Person.new
|
|
|
|
assert !p.valid?
|
|
|
|
assert_equal "This string contains 'single' and \"double\" quotes", p.errors[:karma].last
|
|
|
|
end
|
2009-03-20 18:21:27 -04:00
|
|
|
|
|
|
|
def test_validates_presence_of_for_ruby_class
|
2009-12-23 07:30:58 -05:00
|
|
|
Person.validates_presence_of :karma
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p = Person.new
|
|
|
|
assert p.invalid?
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
assert_equal ["can't be blank"], p.errors[:karma]
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p.karma = "Cold"
|
|
|
|
assert p.valid?
|
2009-03-20 18:21:27 -04:00
|
|
|
end
|
2010-02-25 16:07:48 -05:00
|
|
|
|
2009-08-05 12:44:44 -04:00
|
|
|
def test_validates_presence_of_for_ruby_class_with_custom_reader
|
2009-12-23 07:30:58 -05:00
|
|
|
CustomReader.validates_presence_of :karma
|
2009-08-05 12:44:44 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p = CustomReader.new
|
|
|
|
assert p.invalid?
|
2009-08-05 12:44:44 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
assert_equal ["can't be blank"], p.errors[:karma]
|
2009-08-05 12:44:44 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p[:karma] = "Cold"
|
|
|
|
assert p.valid?
|
2009-08-05 12:44:44 -04:00
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|