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 InclusionValidationTest < 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_inclusion_of
|
|
|
|
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) )
|
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert Topic.new("title" => "a!", "content" => "abc").invalid?
|
|
|
|
assert Topic.new("title" => "a b", "content" => "abc").invalid?
|
|
|
|
assert Topic.new("title" => nil, "content" => "def").invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "a", "content" => "I know you are but what am I?")
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
t.title = "uhoh"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["is not included in the list"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => nil ) }
|
|
|
|
assert_raise(ArgumentError) { Topic.validates_inclusion_of( :title, :in => 0) }
|
|
|
|
|
|
|
|
assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => "hi!" ) }
|
|
|
|
assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => {} ) }
|
|
|
|
assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => [] ) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_inclusion_of_with_allow_nil
|
|
|
|
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :allow_nil=>true )
|
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert Topic.new("title" => "a!", "content" => "abc").invalid?
|
|
|
|
assert Topic.new("title" => "", "content" => "abc").invalid?
|
|
|
|
assert Topic.new("title" => nil, "content" => "abc").valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_validates_inclusion_of_with_formatted_message
|
2010-05-03 02:44:32 -04:00
|
|
|
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => "option %{value} is not in the list" )
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
assert Topic.new("title" => "a", "content" => "abc").valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
t = Topic.new("title" => "uhoh", "content" => "abc")
|
|
|
|
assert t.invalid?
|
2009-03-20 13:36:22 -04:00
|
|
|
assert t.errors[:title].any?
|
|
|
|
assert_equal ["option uhoh is not in the list"], t.errors[:title]
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
2009-03-20 18:21:27 -04:00
|
|
|
def test_validates_inclusion_of_for_ruby_class
|
2009-12-23 07:30:58 -05:00
|
|
|
Person.validates_inclusion_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 = "Lifo"
|
|
|
|
assert p.invalid?
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
assert_equal ["is not included in the list"], p.errors[:karma]
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p.karma = "monkey"
|
|
|
|
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
|