Skip to content
Browse files

Merge pull request #10284 from neerajdotname/missing-active-model-tests

Added tests for if condition in Active Model callbacks
  • Loading branch information...
2 parents 5cffc7e + cf69d52 commit 52ba5a5bec5b2961711be459982b6a58eed63f46 @guilleiguaran guilleiguaran committed Apr 21, 2013
Showing with 21 additions and 0 deletions.
  1. +21 −0 activemodel/test/cases/validations/callbacks_test.rb
View
21 activemodel/test/cases/validations/callbacks_test.rb
@@ -40,8 +40,29 @@ class DogWithMissingName < Dog
validates_presence_of :name
end
+class DogValidatorWithIfCondition < Dog
+ before_validation :set_before_validation_marker1, if: -> { true }
+ before_validation :set_before_validation_marker2, if: -> { false }
+
+ after_validation :set_after_validation_marker1, if: -> { true }
+ after_validation :set_after_validation_marker2, if: -> { false }
+
+ def set_before_validation_marker1; self.history << 'before_validation_marker1'; end
+ def set_before_validation_marker2; self.history << 'before_validation_marker2' ; end
+
+ def set_after_validation_marker1; self.history << 'after_validation_marker1'; end
+ def set_after_validation_marker2; self.history << 'after_validation_marker2' ; end
+end
+
+
class CallbacksWithMethodNamesShouldBeCalled < ActiveModel::TestCase
+ def test_if_condition_is_respected_for_before_validation
+ d = DogValidatorWithIfCondition.new
+ d.valid?
+ assert_equal ["before_validation_marker1", "after_validation_marker1"], d.history
+ end
+
def test_before_validation_and_after_validation_callbacks_should_be_called
d = DogWithMethodCallbacks.new
d.valid?

0 comments on commit 52ba5a5

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