diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-23 16:04:01 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-23 16:04:01 -0800 |
commit | 5f1fc0c8ac6e71bfb4d66da2005b1b0694e18446 (patch) | |
tree | 9db7ddd8d1561b8b85418fdbde4f73381607af23 | |
parent | e8a578224f6abc21c319a6410fe100d15fb77789 (diff) | |
download | rails-5f1fc0c8ac6e71bfb4d66da2005b1b0694e18446.tar.gz rails-5f1fc0c8ac6e71bfb4d66da2005b1b0694e18446.tar.bz2 rails-5f1fc0c8ac6e71bfb4d66da2005b1b0694e18446.zip |
observers leak across tests, so rather than modify the object, we should just count the number of times the observer was called
-rw-r--r-- | activerecord/test/cases/lifecycle_test.rb | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index a42f5d9c9d..6cd8494c9e 100644 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -8,8 +8,12 @@ require 'models/comment' class SpecialDeveloper < Developer; end class DeveloperObserver < ActiveRecord::Observer + def calls + @calls ||= [] + end + def before_save(developer) - developer.salary += 1 + calls << developer end end @@ -202,12 +206,14 @@ class LifecycleTest < ActiveRecord::TestCase assert_equal developer, SalaryChecker.instance.last_saved end - test "callback observing the ancestor does not fire multiple times on descendent" do - DeveloperObserver.instance # activate + def test_observer_is_called_once + observer = DeveloperObserver.instance # activate + observer.calls.clear + developer = Developer.create! :name => 'Ancestor', :salary => 100000 - assert_equal 100001, developer.salary, 'ancestor callback fired multiple times' - developer = SpecialDeveloper.create! :name => 'Descendent', :salary => 100000 - assert_equal 100001, developer.salary, 'descendent callback fired multiple times' + special_developer = SpecialDeveloper.create! :name => 'Descendent', :salary => 100000 + + assert_equal [developer, special_developer], observer.calls end end |