mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #18454 from claudiob/test-on-option-for-amv-callbacks
Add test for AM::Validation::Callbacks with :on
This commit is contained in:
commit
8ee052cf84
1 changed files with 26 additions and 0 deletions
|
@ -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?
|
||||
|
|
Loading…
Reference in a new issue