2009-03-20 11:07:49 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'cases/helper'
|
|
|
|
|
|
|
|
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-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 ) )
|
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert Topic.new("title" => "something", "content" => "abc").valid?
|
|
|
|
assert Topic.new("title" => "monkey", "content" => "abc").invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
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
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert Topic.new("title" => "something", "content" => "abc")
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "monkey")
|
|
|
|
assert t.invalid?
|
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
|
2011-03-11 00:35:23 -05:00
|
|
|
|
|
|
|
def test_validates_exclusion_of_with_lambda
|
|
|
|
Topic.validates_exclusion_of :title, :in => lambda{ |topic| topic.author_name == "sikachu" ? %w( monkey elephant ) : %w( abe wasabi ) }
|
|
|
|
|
|
|
|
p = Topic.new
|
|
|
|
p.title = "elephant"
|
|
|
|
p.author_name = "sikachu"
|
|
|
|
assert p.invalid?
|
|
|
|
|
|
|
|
p.title = "wasabi"
|
|
|
|
assert p.valid?
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|