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/person'
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
class ExclusionValidationTest < ActiveModel::TestCase
|
2009-03-20 12:02:12 -04:00
|
|
|
include ActiveModel::TestsDatabase
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
def teardown
|
|
|
|
Topic.reset_callbacks(:validate)
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
def test_validates_exclusion_of
|
|
|
|
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ) )
|
|
|
|
|
|
|
|
assert Topic.create("title" => "something", "content" => "abc").valid?
|
|
|
|
assert !Topic.create("title" => "monkey", "content" => "abc").valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_exclusion_of_with_formatted_message
|
2010-05-03 02:44:32 -04:00
|
|
|
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => "option %{value} is restricted" )
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
assert Topic.create("title" => "something", "content" => "abc")
|
|
|
|
|
|
|
|
t = Topic.create("title" => "monkey")
|
|
|
|
assert !t.valid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["option monkey is restricted"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
2009-03-20 18:21:27 -04:00
|
|
|
|
|
|
|
def test_validates_exclusion_of_for_ruby_class
|
2009-12-23 07:30:58 -05:00
|
|
|
Person.validates_exclusion_of :karma, :in => %w( abe monkey )
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p = Person.new
|
|
|
|
p.karma = "abe"
|
|
|
|
assert p.invalid?
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
assert_equal ["is reserved"], p.errors[:karma]
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p.karma = "Lifo"
|
|
|
|
assert p.valid?
|
|
|
|
ensure
|
|
|
|
Person.reset_callbacks(:validate)
|
2009-03-20 18:21:27 -04:00
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|