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:
parent
e8a578224f
commit
5f1fc0c8ac
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue