diff options
Diffstat (limited to 'activemodel/test/cases/validations/conditional_validation_test.rb')
-rw-r--r-- | activemodel/test/cases/validations/conditional_validation_test.rb | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/activemodel/test/cases/validations/conditional_validation_test.rb b/activemodel/test/cases/validations/conditional_validation_test.rb index 6866bfcf24..3cb95b4a00 100644 --- a/activemodel/test/cases/validations/conditional_validation_test.rb +++ b/activemodel/test/cases/validations/conditional_validation_test.rb @@ -1,21 +1,19 @@ # encoding: utf-8 require 'cases/helper' -require 'cases/tests_database' require 'models/topic' class ConditionalValidationTest < ActiveModel::TestCase - include ActiveModel::TestsDatabase def teardown Topic.reset_callbacks(:validate) end - + def test_if_validation_using_method_true # When the method returns true Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => :condition_is_true ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 5"], t.errors["title"] end @@ -23,15 +21,15 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_method_true # When the method returns true Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => :condition_is_true ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? - assert !t.errors[:title].any? + assert t.errors[:title].empty? end def test_if_validation_using_method_false # When the method returns false Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => :condition_is_true_but_its_not ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? end @@ -39,8 +37,8 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_method_false # When the method returns false Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => :condition_is_true_but_its_not ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 5"], t.errors["title"] end @@ -48,8 +46,8 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_if_validation_using_string_true # When the evaluated string returns true Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => "a = 1; a == 1" ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 5"], t.errors["title"] end @@ -57,7 +55,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_string_true # When the evaluated string returns true Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => "a = 1; a == 1" ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? end @@ -65,7 +63,7 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_if_validation_using_string_false # When the evaluated string returns false Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => "false") - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? end @@ -73,8 +71,8 @@ class ConditionalValidationTest < ActiveModel::TestCase def test_unless_validation_using_string_false # When the evaluated string returns false Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => "false") - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 5"], t.errors["title"] end @@ -83,8 +81,8 @@ class ConditionalValidationTest < ActiveModel::TestCase # When the block returns true Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => Proc.new { |r| r.content.size > 4 } ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 5"], t.errors["title"] end @@ -93,7 +91,7 @@ class ConditionalValidationTest < ActiveModel::TestCase # When the block returns true Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => Proc.new { |r| r.content.size > 4 } ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? end @@ -102,7 +100,7 @@ class ConditionalValidationTest < ActiveModel::TestCase # When the block returns false Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => Proc.new { |r| r.title != "uhohuhoh"} ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") assert t.valid? assert t.errors[:title].empty? end @@ -111,8 +109,8 @@ class ConditionalValidationTest < ActiveModel::TestCase # When the block returns false Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => Proc.new { |r| r.title != "uhohuhoh"} ) - t = Topic.create("title" => "uhohuhoh", "content" => "whatever") - assert !t.valid? + t = Topic.new("title" => "uhohuhoh", "content" => "whatever") + assert t.invalid? assert t.errors[:title].any? assert_equal ["hoo 5"], t.errors["title"] end @@ -132,7 +130,7 @@ class ConditionalValidationTest < ActiveModel::TestCase assert t.valid?, "A topic with a basic title should be valid" t.title = "A very important title" - assert !t.valid?, "A topic with an important title, but without an author, should not be valid" + assert t.invalid?, "A topic with an important title, but without an author, should not be valid" assert t.errors[:author_name].any?, "A topic with an 'important' title should require an author" t.author_name = "Hubert J. Farnsworth" |