Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4918

Show
Ignore:
Timestamp:
09/03/06 17:54:48 (2 years ago)
Author:
bitsweat
Message:

Validation tests

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/test/validations_test.rb

    r4596 r4918  
    2727    r = Reply.new 
    2828    r.title = "There's no content!" 
    29     assert !r.save, "A reply without content shouldn't be saveable" 
     29    assert !r.valid?, "A reply without content shouldn't be saveable" 
    3030 
    3131    r.content = "Messa content!" 
    32     assert r.save, "A reply with content should be saveable" 
     32    assert r.valid?, "A reply with content should be saveable" 
    3333  end 
    3434 
     
    3636    r = Reply.new 
    3737    r.title = "There's no content!" 
    38     r.save 
     38    assert !r.valid? 
    3939    assert r.errors.invalid?("content"), "A reply without content should mark that attribute as invalid" 
    4040    assert_equal "Empty", r.errors.on("content"), "A reply without content should contain an error" 
     
    4444  def test_double_attr_validation_and_error_msg 
    4545    r = Reply.new 
    46     assert !r.save 
     46    assert !r.valid? 
    4747 
    4848    assert r.errors.invalid?("title"), "A reply without title should mark that attribute as invalid" 
     
    5858    r = Reply.new 
    5959    r.title = "Wrong Create" 
    60     assert !r.save 
     60    assert !r.valid? 
    6161    assert r.errors.invalid?("title"), "A reply with a bad title should mark that attribute as invalid" 
    6262    assert_equal "is Wrong Create", r.errors.on("title"), "A reply with a bad content should contain an error" 
     
    173173  end 
    174174 
    175   def test_title_confirmation_no_confirm 
     175  def test_no_title_confirmation 
    176176    Topic.validates_confirmation_of(:title) 
    177177 
    178     t = Topic.create("title" => "We should not be confirmed") 
    179     assert t.save 
     178    t = Topic.new(:author_name => "Plutarch") 
     179    assert t.valid? 
     180 
     181    t.title_confirmation = "Parallel Lives" 
     182    assert t.valid? 
     183 
     184    t.title_confirmation = nil 
     185    t.title = "Parallel Lives" 
     186    assert !t.valid? 
     187 
     188    t.title_confirmation = "Parallel Lives" 
     189    assert t.valid? 
    180190  end 
    181191 
     
    710720      t = Topic.create("title" => "䞀二䞉四五", "content" => "whatever") 
    711721      assert t.valid? 
    712        
     722 
    713723      t.title = "䞀二34五å 
    714724­" 
     
    756766      assert t.errors.on(:title) 
    757767      assert_equal "長すぎたす: 10", t.errors[:title] 
    758        
     768 
    759769      t.title = "䞀二䞉四五å 
    760770­äžƒå 
    761771«ä¹" 
    762772      assert t.save 
    763        
     773 
    764774      t.title = "䞀二3" 
    765775      assert t.save 
    766        
     776 
    767777      t.content = "䞀二䞉四五å 
    768778­äžƒå 
    769779«ä¹å" 
    770780      assert t.save 
    771        
     781 
    772782      t.content = t.title = "䞀二䞉四五å 
    773783­" 
     
    783793      assert !t.save 
    784794      assert t.errors.on(:title) 
    785        
     795 
    786796      t.title = "1二䞉4" 
    787797      assert !t.save 
    788798      assert t.errors.on(:title) 
    789799      assert_equal "短すぎたす: 5", t.errors[:title] 
    790        
     800 
    791801      t.title = "valid" 
    792802      t.content = "䞀二䞉四五å 
     
    795805      assert !t.save 
    796806      assert t.errors.on(:content) 
    797        
     807 
    798808      t.content = "䞀二345" 
    799809      assert t.save 
     
    864874 
    865875  def test_throw_away_typing 
    866     d = Developer.create "name" => "David", "salary" => "100,000" 
     876    d = Developer.new("name" => "David", "salary" => "100,000") 
    867877    assert !d.valid? 
    868878    assert_equal 100, d.salary 
     
    879889 
    880890  def test_validates_confirmation_of_with_custom_error_using_quotes 
    881     Developer.validates_confirmation_of :name, :message=> "This string contains 'single' and \"double\" quotes" 
     891    Developer.validates_confirmation_of :name, :message=> "confirm 'single' and \"double\" quotes" 
    882892    d = Developer.new 
    883893    d.name = "John" 
    884894    d.name_confirmation = "Johnny" 
    885895    assert !d.valid? 
    886     assert_equal d.errors.on(:name), "This string contains 'single' and \"double\" quotes" 
     896    assert_equal "confirm 'single' and \"double\" quotes", d.errors.on(:name) 
    887897  end 
    888898 
    889899  def test_validates_format_of_with_custom_error_using_quotes 
    890     Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message=> "This string contains 'single' and \"double\" quotes" 
     900    Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message=> "format 'single' and \"double\" quotes" 
    891901    d = Developer.new 
    892     d.name = "John 32" 
     902    d.name = d.name_confirmation = "John 32" 
    893903    assert !d.valid? 
    894     assert_equal d.errors.on(:name), "This string contains 'single' and \"double\" quotes" 
     904    assert_equal "format 'single' and \"double\" quotes", d.errors.on(:name) 
    895905  end 
    896906 
     
    900910    d.salary = "90,000" 
    901911    assert !d.valid? 
    902     assert_equal d.errors.on(:salary).first, "This string contains 'single' and \"double\" quotes" 
     912    assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:salary).first 
    903913  end 
    904914 
     
    908918    d.name = "Jeffrey" 
    909919    assert !d.valid? 
    910     assert_equal d.errors.on(:name).first, "This string contains 'single' and \"double\" quotes" 
     920    assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).first 
    911921  end 
    912922 
     
    916926    d.name = "Joe" 
    917927    assert !d.valid? 
    918     assert_equal d.errors.on(:name).first, "This string contains 'single' and \"double\" quotes" 
     928    assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).first 
    919929  end 
    920930 
     
    924934    d.name = "Joe" 
    925935    assert !d.valid? 
    926     assert_equal d.errors.on(:name).first, "This string contains 'single' and \"double\" quotes" 
     936    assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).first 
    927937  end 
    928938 
     
    932942    d.name = "Joe" 
    933943    assert !d.valid? 
    934     assert_equal d.errors.on(:non_existent), "This string contains 'single' and \"double\" quotes" 
     944    assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:non_existent) 
    935945  end 
    936946 
     
    940950    d.name = "David" 
    941951    assert !d.valid? 
    942     assert_equal d.errors.on(:name).first, "This string contains 'single' and \"double\" quotes" 
     952    assert_equal "This string contains 'single' and \"double\" quotes", d.errors.on(:name).first 
    943953  end 
    944954 
     
    949959    r.topic = Topic.create("title" => "uhohuhoh") 
    950960    assert !r.valid? 
    951     assert_equal r.errors.on(:topic).first, "This string contains 'single' and \"double\" quotes" 
     961    assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic).first 
    952962  end 
    953963 
     
    966976    t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 
    967977    assert t.valid? 
    968     assert !t.errors.on(:title)       
     978    assert !t.errors.on(:title) 
    969979  end 
    970980