observers leak across tests, so rather than modify the object, we should just count the number of times the observer was called

This commit is contained in:
Aaron Patterson 2011-02-23 16:04:01 -08:00
parent e8a578224f
commit 5f1fc0c8ac
1 changed files with 12 additions and 6 deletions

View File

@ -8,8 +8,12 @@ require 'models/comment'
class SpecialDeveloper < Developer; end class SpecialDeveloper < Developer; end
class DeveloperObserver < ActiveRecord::Observer class DeveloperObserver < ActiveRecord::Observer
def calls
@calls ||= []
end
def before_save(developer) def before_save(developer)
developer.salary += 1 calls << developer
end end
end end
@ -202,12 +206,14 @@ class LifecycleTest < ActiveRecord::TestCase
assert_equal developer, SalaryChecker.instance.last_saved assert_equal developer, SalaryChecker.instance.last_saved
end end
test "callback observing the ancestor does not fire multiple times on descendent" do def test_observer_is_called_once
DeveloperObserver.instance # activate observer = DeveloperObserver.instance # activate
observer.calls.clear
developer = Developer.create! :name => 'Ancestor', :salary => 100000 developer = Developer.create! :name => 'Ancestor', :salary => 100000
assert_equal 100001, developer.salary, 'ancestor callback fired multiple times' special_developer = SpecialDeveloper.create! :name => 'Descendent', :salary => 100000
developer = SpecialDeveloper.create! :name => 'Descendent', :salary => 100000
assert_equal 100001, developer.salary, 'descendent callback fired multiple times' assert_equal [developer, special_developer], observer.calls
end end
end end