Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merge callbacks_observers_test.rb into lifecycle_test.rb where other …

…observers test reside

Benefits: test able to run independently, subclassing instead of changing the original Comment model

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit cf616e48765d1aa378d04618c044b493fd9583e1 1 parent 779723a
@mislav mislav authored jeremy committed
View
37 activerecord/test/cases/callbacks_observers_test.rb
@@ -1,37 +0,0 @@
-require "cases/helper"
-
-class Comment < ActiveRecord::Base
- attr_accessor :callers
-
- before_validation :record_callers
-
- after_validation do
- record_callers
- end
-
- def record_callers
- callers << self.class if callers
- end
-end
-
-class CommentObserver < ActiveRecord::Observer
- attr_accessor :callers
-
- def after_validation(model)
- callers << self.class if callers
- end
-end
-
-class CallbacksObserversTest < ActiveRecord::TestCase
- def test_model_callbacks_fire_before_observers_are_notified
- callers = []
-
- comment = Comment.new
- comment.callers = callers
-
- CommentObserver.instance.callers = callers
-
- comment.valid?
- assert_equal [Comment, Comment, CommentObserver], callers, "model callbacks did not fire before observers were notified"
- end
-end
View
34 activerecord/test/cases/lifecycle_test.rb
@@ -3,6 +3,7 @@
require 'models/developer'
require 'models/reply'
require 'models/minimalistic'
+require 'models/comment'
class SpecialDeveloper < Developer; end
@@ -57,6 +58,28 @@ def after_find(record)
end
end
+class ValidatedComment < Comment
+ attr_accessor :callers
+
+ before_validation :record_callers
+
+ after_validation do
+ record_callers
+ end
+
+ def record_callers
+ callers << self.class if callers
+ end
+end
+
+class ValidatedCommentObserver < ActiveRecord::Observer
+ attr_accessor :callers
+
+ def after_validation(model)
+ callers << self.class if callers
+ end
+end
+
class LifecycleTest < ActiveRecord::TestCase
fixtures :topics, :developers, :minimalistics
@@ -125,4 +148,15 @@ def test_after_find_can_be_observed_when_its_defined_on_the_model
def test_invalid_observer
assert_raise(ArgumentError) { Topic.observers = Object.new; Topic.instantiate_observers }
end
+
+ test "model callbacks fire before observers are notified" do
+ callers = []
+
+ comment = ValidatedComment.new
+ comment.callers = ValidatedCommentObserver.instance.callers = callers
+
+ comment.valid?
+ assert_equal [ValidatedComment, ValidatedComment, ValidatedCommentObserver], callers,
+ "model callbacks did not fire before observers were notified"
+ end
end

0 comments on commit cf616e4

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