diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-16 03:17:43 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2004-12-16 03:17:43 +0000 |
commit | f7f1fee7655d444269ad4b387c45cd4ed576809b (patch) | |
tree | e9501064d80d8d9e442e914386b1a79508264c63 /activerecord/test | |
parent | d8a58eea1118fa5ca9339a677b5179ed712f9246 (diff) | |
download | rails-f7f1fee7655d444269ad4b387c45cd4ed576809b.tar.gz rails-f7f1fee7655d444269ad4b387c45cd4ed576809b.tar.bz2 rails-f7f1fee7655d444269ad4b387c45cd4ed576809b.zip |
Added Base.validates_inclusion_of that validates whether the value of the specified attribute is available in a particular enumerable object. [what-a-day]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@178 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-x | activerecord/test/validations_test.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index d4c77d9953..92c0a71c35 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -220,4 +220,27 @@ class ValidationsTest < Test::Unit::TestCase assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) } end + + def test_validates_inclusion_of + Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) ) + + assert !Topic.create("title" => "a!", "content" => "abc").valid? + assert !Topic.create("title" => "a b", "content" => "abc").valid? + assert !Topic.create("title" => nil, "content" => "def").valid? + assert !Topic.create("title" => %w(a b c), "content" => "def").valid? + + t = Topic.create("title" => "a", "content" => "I know you are but what am I?") + assert t.valid? + t.title = "uhoh" + assert !t.valid? + assert t.errors.on(:title) + assert_equal "is not included in the list", t.errors["title"] + + 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 end |