Sha256: 8c76ddd682eee9d56366f8a616d5db42ff42bac4dfd54663e311bf6bbf26c42b

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require File.dirname(__FILE__) + '/test_helper'

class TrackChangesTest < Test::Unit::TestCase
  load_schema

  def test_schema_has_loaded_correctly
    assert_equal [], Audit.all
    assert_equal [], Post.all
    assert_equal [], User.all
  end

  def test_should_add_track_changes_method_to_models
    assert Post.new.respond_to?(:current_user)
    assert Post.new.respond_to?(:current_user=)
  end

  def test_should_add_association_to_models
    assert Post.new.respond_to?(:audits)
    assert_equal [], Post.new.audits.all
  end

  def test_should_audit_changes
    user = User.create!(:name => "John Public")
    post = Post.create!(:title => "Test Post", :body => "Hello, world!")

    post.update_attributes(:title => "Title Changed")

    audit = post.audits.desc.first
    expected_change = {"title" => ["Test Post", "Title Changed"]}

    assert_equal 1, post.audits.count
    assert_equal expected_change, audit.change_set
  end

  def test_should_not_audit_creation
    old_count = Audit.count
    User.create!(:name => "John Public")
    Post.create!(:title => "Test Post", :body => "Hello, world!")

    assert_equal old_count, Audit.count, Audit.all.map {|a| a.change_set.inspect }.join(" ")
  end
  
  def test_should_not_save_empty_change_set
    old_count = Audit.count
    user = User.create!(:name => "John Public")
    post = Post.create!(:title => "Test Post", :body => "Hello, world!")
    
    post.update_attributes(:title => "Test Post") # Same title
    assert_equal old_count, Audit.count
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
track_changes-1.0.1 test/track_changes_test.rb