From a81bd92ba6cae342c0e0498245f5a3038775bf59 Mon Sep 17 00:00:00 2001 From: Yi Wen Date: Sun, 30 Oct 2011 09:41:52 -0500 Subject: [PATCH] added tests for testing that validates_with_block with :when option works properly --- spec/unit/validators/block_validator_spec.rb | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 spec/unit/validators/block_validator_spec.rb diff --git a/spec/unit/validators/block_validator_spec.rb b/spec/unit/validators/block_validator_spec.rb new file mode 100644 index 00000000..30fe219c --- /dev/null +++ b/spec/unit/validators/block_validator_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe 'DataMapper::Validations::ValidatesWithBlock' do + describe "#validates_with_block" do + let(:klass) { + Class.new do + include DataMapper::Resource + property :id, DataMapper::Property::Serial + property :name, String + validates_with_block(:name, :when => [ :adding ]) do + if ( name == "Special") + [false, "fail"] + else + true + end + end + end + } + subject {klass.new} + context "when validate in a common situation" do + it "should be valid" do + subject.name = "Special" + subject.should be_valid + end + end + context "when validate in a adding" do + it "should be valid" do + subject.name = "Special" + subject.should_not be_valid(:adding) + end + end + end +end