0
@@ -2,7 +2,7 @@ require 'pathname'
0
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
0
describe DataMapper::Validate do
0
include DataMapper::Resource
0
property :id, Integer, :serial => true
0
@@ -12,6 +12,69 @@ describe DataMapper::Validate do
0
+ it 'should respond to save' do
0
+ Yacht.new.should respond_to(:save)
0
+ @yacht = Yacht.new :name => 'The Gertrude'
0
+ describe 'without context specified' do
0
+ it 'should validate using the default context' do
0
+ @yacht.should_receive(:valid?).with(:default)
0
+ it 'should save if the object is valid for the default context' do
0
+ @yacht.should be_valid
0
+ @yacht.save.should be_true
0
+ @yacht.should_not be_new_record
0
+ it 'should not save if the object is not valid for the default context' do
0
+ @yacht.should be_valid
0
+ @yacht.should_not be_valid
0
+ @yacht.save.should be_false
0
+ @yacht.should be_new_record
0
+ describe 'with context specified' do
0
+ validates_length :name, :min => 2, :context => [ :strict_name ]
0
+ it 'should validate using the specified context' do
0
+ @yacht.should_receive(:valid?).with(:strict_name)
0
+ @yacht.save(:strict_name)
0
+ it 'should save if the object is valid for the specified context' do
0
+ @yacht.should be_valid(:strict_name)
0
+ @yacht.save(:strict_name).should be_true
0
+ @yacht.should_not be_new_record
0
+ it 'should not save if the object is not valid for the specified context' do
0
+ @yacht.should be_valid(:strict_name)
0
+ @yacht.should_not be_valid(:strict_name)
0
+ @yacht.save(:strict_name).should be_false
0
+ @yacht.should be_new_record
0
it "should respond to validatable? (for recursing assocations)" do
0
Yacht.new.should be_validatable
0
Class.new.new.should_not be_validatable
0
@@ -30,17 +93,16 @@ describe DataMapper::Validate do
0
Yacht.validators.should respond_to(:execute)
0
- it "should place a validator in the :default context if a named context is
0
+ it "should place a validator in the :default context if a named context is not provided" do
0
Yacht.validators.context(:default).length.should == 2
0
it "should allow multiple user defined contexts for a validator" do
0
property :port, String, :auto_validation => false
0
validates_present :port, :context => [:at_sea, :in_harbor]
0
Yacht.validators.context(:at_sea).length.should == 1
0
Yacht.validators.context(:in_harbor).length.should == 1
0
Yacht.validators.context(:no_such_context).length.should == 0
0
@@ -115,83 +177,94 @@ describe DataMapper::Validate do
0
sea.errors.full_messages.first.should == 'Year built is a must enter field'
0
- it "should execute a Proc when provided in an :if clause and run validation
0
- if the Proc returns true" do
0
+ it "should execute a Proc when provided in an :if clause and run validation if the Proc returns true" do
0
include DataMapper::Resource
0
property :id, Integer, :serial => true
0
property :owner, String, :auto_validation => false
0
validates_present :owner, :if => Proc.new{|resource| resource.owned?}
0
- def owned?; false; end
0
Dingy.new.valid?.should == true
0
Dingy.new.valid?.should_not == true
0
- it "should execute a symbol or method name provided in an :if clause and run
0
- validation if the method returns true" do
0
+ it "should execute a symbol or method name provided in an :if clause and run validation if the method returns true" do
0
validates_present :owner, :if => :owned?
0
- def owned?; false; end
0
Dingy.new.valid?.should == true
0
Dingy.new.valid?.should_not == true
0
- it "should execute a Proc when provided in an :unless clause and not run
0
- validation if the Proc returns true" do
0
+ it "should execute a Proc when provided in an :unless clause and not run validation if the Proc returns true" do
0
include DataMapper::Resource
0
property :id, Integer, :serial => true
0
validates_present :salesman, :unless => Proc.new{|resource| resource.sold?}
0
RowBoat.new.valid?.should_not == true
0
RowBoat.new.valid?.should == true
0
- it "should execute a symbol or method name provided in an :unless clause and
0
- not run validation if the method returns true" do
0
+ it "should execute a symbol or method name provided in an :unless clause and not run validation if the method returns true" do
0
validates_present :salesman, :unless => :sold?
0
Dingy.new.valid?.should_not == true #not sold and no salesman
0
Dingy.new.valid?.should == true # sold and no salesman
0
- it "should perform automatic recursive validation #all_valid? checking all
0
- instance variables (and ivar.each items if valid)" do
0
+ it "should perform automatic recursive validation #all_valid? checking all instance variables (and ivar.each items if valid)" do
0
include DataMapper::Resource
0
property :id, Integer, :serial => true
0
@@ -249,6 +322,5 @@ describe DataMapper::Validate do
0
invoice.line_items[1].price = '23.44'
0
invoice.all_valid?.should == true