Skip to content
Browse files

observers leak across tests, so rather than modify the object, we sho…

…uld just count the number of times the observer was called
  • Loading branch information...
1 parent ef31b87 commit 77d4211a62ae958121a17a8bb8e89a00083bd6df @tenderlove tenderlove committed Feb 23, 2011
Showing with 12 additions and 6 deletions.
  1. +12 −6 activerecord/test/cases/lifecycle_test.rb
View
18 activerecord/test/cases/lifecycle_test.rb
@@ -8,8 +8,12 @@
class SpecialDeveloper < Developer; end
class DeveloperObserver < ActiveRecord::Observer
+ def calls
+ @calls ||= []
+ end
+
def before_save(developer)
- developer.salary += 1
+ calls << developer
end
end
@@ -201,12 +205,14 @@ def test_invalid_observer
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

0 comments on commit 77d4211

Please sign in to comment.
Something went wrong with that request. Please try again.