Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added tests for if condition in Active Model callbacks
  • Loading branch information
Neeraj Singh committed Apr 21, 2013
1 parent a489bfe commit cf69d52
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions activemodel/test/cases/validations/callbacks_test.rb
Expand Up @@ -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?
Expand Down

0 comments on commit cf69d52

Please sign in to comment.