require File.expand_path(File.dirname(__FILE__) + '/abstract_unit') class ModelObserverTest < Test::Unit::TestCase def test_create_observed p1, p2 = Person.new(:name => 'One'), Person.new(:name => 'Two') assert_model_changes 2 do assert_equal [p1, p2], ModelObserver.collect { [p1, p2].each(&:save!) } end end def test_update_observed assert_model_changes 2 do objects = ModelObserver.collect do people(:jonathan).update_attributes!(:name => 'New name') people(:david).update_attributes(:name => 'New name') end assert_equal [people(:jonathan), people(:david)], objects end end def test_update_ignores_unchanged_models assert_no_model_changes do people(:jonathan).save! end end def test_ignore_model_changes assert_no_model_changes do ModelObserver.ignore do Person.create! end end end def test_collect_exclusively assert_model_changes 1 do Person.create! exclusive_objects = ModelObserver.collect_exclusively do person = Person.create! end assert_equal 1, exclusive_objects.size end end def test_collect_exclusively_inside_ignore assert_no_model_changes do ModelObserver.ignore do exclusive_objects = ModelObserver.collect_exclusively do Person.create! end assert_equal 1, exclusive_objects.size end end end def test_no_model_changes_in_failed_transaction assert_no_model_changes do begin Person.transaction do Person.create! Person.create! assert false end rescue Test::Unit::AssertionFailedError else raise "should not be here" end end end def test_transaction_returns_result_of_block assert_equal 5, Person.transaction { 5 } end end