aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorclaudiob <claudiob@gmail.com>2015-01-11 12:25:53 -0800
committerclaudiob <claudiob@gmail.com>2015-01-11 12:29:39 -0800
commit684cbee4733d47fe242b5637d6bdc48ec16ac536 (patch)
tree460f848e983418784356c7395674a288838c3816 /activemodel
parent72139d8d310d896db78eaec98582c7a638135102 (diff)
downloadrails-684cbee4733d47fe242b5637d6bdc48ec16ac536.tar.gz
rails-684cbee4733d47fe242b5637d6bdc48ec16ac536.tar.bz2
rails-684cbee4733d47fe242b5637d6bdc48ec16ac536.zip
Add test for AM::Validation::Callbacks with :on
`before_validation` and `after_validation` from ActiveModel::Validation::Callbacks accept an optional `:on` parameter that was not previously documented or tested. For instance given before_validation :do_something, on: :create then `object.valid?(:create)` will invoke `:do_something` while `object.valid?` or `object.valid?(:anything_else)` will not.
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/test/cases/validations/callbacks_test.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/activemodel/test/cases/validations/callbacks_test.rb b/activemodel/test/cases/validations/callbacks_test.rb
index 4b0dd58efb..cc50ffbbef 100644
--- a/activemodel/test/cases/validations/callbacks_test.rb
+++ b/activemodel/test/cases/validations/callbacks_test.rb
@@ -50,6 +50,14 @@ class DogWithMissingName < Dog
validates_presence_of :name
end
+class DogValidatorWithOnCondition < Dog
+ before_validation :set_before_validation_marker, on: :create
+ after_validation :set_after_validation_marker, on: :create
+
+ def set_before_validation_marker; self.history << 'before_validation_marker'; end
+ def set_after_validation_marker; self.history << 'after_validation_marker' ; end
+end
+
class DogValidatorWithIfCondition < Dog
before_validation :set_before_validation_marker1, if: -> { true }
before_validation :set_before_validation_marker2, if: -> { false }
@@ -73,6 +81,24 @@ class CallbacksWithMethodNamesShouldBeCalled < ActiveModel::TestCase
assert_equal ["before_validation_marker1", "after_validation_marker1"], d.history
end
+ def test_on_condition_is_respected_for_validation_with_matching_context
+ d = DogValidatorWithOnCondition.new
+ d.valid?(:create)
+ assert_equal ["before_validation_marker", "after_validation_marker"], d.history
+ end
+
+ def test_on_condition_is_respected_for_validation_without_matching_context
+ d = DogValidatorWithOnCondition.new
+ d.valid?(:save)
+ assert_equal [], d.history
+ end
+
+ def test_on_condition_is_respected_for_validation_without_context
+ d = DogValidatorWithOnCondition.new
+ d.valid?
+ assert_equal [], d.history
+ end
+
def test_before_validation_and_after_validation_callbacks_should_be_called
d = DogWithMethodCallbacks.new
d.valid?