Sha256: c9049dc671680ffaaa22c0a1829a752579ab1056766b93d8b3bbf6191be91b98

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require "test_helper"

class ValidationsTest < ActiveSupport::TestCase
  
  def test_basic
    account = Account.new
    assert !account.valid?
    assert_equal 1, account.errors.length
    assert_equal ["can't be blank"], account.errors[:name]
  end
  
  def test_create
    account = Account.create
    assert account.new_record?
    assert_equal({}, account.attributes)
    assert_equal [:before_validation_on_create, :before_validation], account.callbacks_made
  end
  
  def test_create!
    begin
      Account.create!
      assert false
    rescue CurlyMustache::RecordInvalid => e
      assert e.message.index("Validation failed:") == 0
    end
  end
  
  def test_save
    account = Account.new
    assert_equal false, account.save
    assert_equal({}, account.attributes)
    assert_equal [:before_validation_on_create, :before_validation], account.callbacks_made
    
    # Now make sure the callback changed to on_update.
    account = Account.create!(:name => "blah")
    account.callbacks_made = []
    account.name = nil
    assert_equal false, account.save
    assert_equal [:before_validation_on_update, :before_validation], account.callbacks_made
  end
  
  def test_save!
    begin
      account = Account.new
      account.save!
      assert false
    rescue CurlyMustache::RecordInvalid => e
      assert e.message.index("Validation failed:") == 0
      assert_equal [:before_validation_on_create, :before_validation], account.callbacks_made
    end
    
    # Now make sure the callback changed to on_update.
    account = Account.create!(:name => "blah")
    account.callbacks_made = []
    begin
      account.name = nil
      account.save!
      assert false
    rescue CurlyMustache::RecordInvalid => e
      assert e.message.index("Validation failed:") == 0
      assert_equal [:before_validation_on_update, :before_validation], account.callbacks_made
    end
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
curly_mustache-0.1.0 test/validations_test.rb