diff options
author | Neeraj Singh <neerajdotname@gmail.com> | 2010-06-16 11:30:37 -0400 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-06-19 17:59:52 +0200 |
commit | 51739d3228d12907d60fb1b0a2b1ef96c55f66a3 (patch) | |
tree | 428364caab82ea2cfd5f320f5074bf3646b332a2 /activemodel/test/cases | |
parent | 312f43324159fbcd8749cd331ed7d6500a714a83 (diff) | |
download | rails-51739d3228d12907d60fb1b0a2b1ef96c55f66a3.tar.gz rails-51739d3228d12907d60fb1b0a2b1ef96c55f66a3.tar.bz2 rails-51739d3228d12907d60fb1b0a2b1ef96c55f66a3.zip |
moving before_validation and after_validation functionality from ActiveRecord to ActiveModel
[#4653 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activemodel/test/cases')
-rw-r--r-- | activemodel/test/cases/validations/callbacks_test.rb | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/activemodel/test/cases/validations/callbacks_test.rb b/activemodel/test/cases/validations/callbacks_test.rb new file mode 100644 index 0000000000..08dcf254c2 --- /dev/null +++ b/activemodel/test/cases/validations/callbacks_test.rb @@ -0,0 +1,76 @@ +# encoding: utf-8 +require 'cases/helper' + +class Dog + include ActiveModel::Validations + + attr_accessor :name, :history + + def history + @history ||= [] + end +end + +class DogWithMethodCallbacks < Dog + before_validation :set_before_validation_marker + after_validation :set_after_validation_marker + + def set_before_validation_marker; self.history << 'before_validation_marker'; end + def set_after_validation_marker; self.history << 'after_validation_marker' ; end +end + +class DogValidtorsAreProc < Dog + before_validation { self.history << 'before_validation_marker' } + after_validation { self.history << 'after_validation_marker' } +end + +class DogWithTwoValidators < Dog + before_validation { self.history << 'before_validation_marker1' } + before_validation { self.history << 'before_validation_marker2' } +end + +class DogValidatorReturningFalse < Dog + before_validation { false } + before_validation { self.history << 'before_validation_marker2' } +end + +class DogWithMissingName < Dog + before_validation { self.history << 'before_validation_marker' } + validates_presence_of :name +end + +class CallbacksWithMethodNamesShouldBeCalled < ActiveModel::TestCase + + def test_before_validation_and_after_validation_callbacks_should_be_called + d = DogWithMethodCallbacks.new + d.valid? + assert_equal ['before_validation_marker', 'after_validation_marker'], d.history + end + + def test_before_validation_and_after_validation_callbacks_should_be_called_with_proc + d = DogValidtorsAreProc.new + d.valid? + assert_equal ['before_validation_marker', 'after_validation_marker'], d.history + end + + def test_before_validation_and_after_validation_callbacks_should_be_called_in_declared_order + d = DogWithTwoValidators.new + d.valid? + assert_equal ['before_validation_marker1', 'before_validation_marker2'], d.history + end + + def test_further_callbacks_should_not_be_called_if_before_validation_returns_false + d = DogValidatorReturningFalse.new + output = d.valid? + assert_equal [], d.history + assert_equal false, output + end + + def test_validation_test_should_be_done + d = DogWithMissingName.new + output = d.valid? + assert_equal ['before_validation_marker'], d.history + assert_equal false, output + end + +end |