public
Rubygem
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-more.git
Search Repo:
myabc (author)
Thu May 08 06:46:07 -0700 2008
commit  d030592d948d24d86511bc03af0026d7ebebce64
tree    06627dd67de4db6f2becbc5c8e1934721d682869
parent  53973fb564838e387c4721a46b3bba7d2fa10bcf
dm-more / dm-validations / spec / acceptance_validator_spec.rb
100644 76 lines (75 sloc) 2.229 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'pathname'
require Pathname(__FILE__).dirname.expand_path + 'spec_helper'
 
describe DataMapper::Validate::AcceptanceValidator do
  describe "with standard options" do
    before :all do
      class SkimBat
        include DataMapper::Validate
        validates_acceptance_of :sailyness
        attr_accessor :sailyness
      end
      @s = SkimBat.new
    end
    it "should validate if a resource instance has accepted" do
      @s.sailyness = "1"
      @s.valid?.should == true
    end
    it "should not validate if a resource instance has not accepted" do
      @s.sailyness = "0"
      @s.valid?.should == false
    end
    it "should allow nil acceptance" do
      @s.sailyness = nil
      @s.valid?.should == true
    end
    it "should add the default message when invalid" do
      @s.sailyness = "0"
      @s.valid?.should == false
      @s.errors.full_messages.join(" ").should =~ /#{DataMapper::Validate::AcceptanceValidator.default_message_for_field("sailyness")}/
    end
  end
  describe "with :allow_nil => false" do
    before :all do
      SkimBat.class_eval do
        validators.clear!
        validates_acceptance_of :sailyness, :allow_nil => false
      end
      @s = SkimBat.new
    end
    it "should not allow nil acceptance" do
      @s.sailyness = nil
      @s.valid?.should == false
    end
  end
  describe "with custom :accept" do
    before :all do
      SkimBat.class_eval do
        validators.clear!
        validates_acceptance_of :sailyness, :accept => "true"
      end
      @s = SkimBat.new
    end
    it "should validate if a resource instance has accepted" do
      @s.sailyness = "true"
      @s.valid?.should == true
    end
    it "should not validate if a resource instance has not accepted" do
      @s.sailyness = "false"
      @s.valid?.should == false
    end
  end
  describe "with custom message" do
    before :all do
      SkimBat.class_eval do
        validators.clear!
        validates_acceptance_of :sailyness, :message => "hehu!"
      end
      @s = SkimBat.new
    end
    it "should append the custom message when invalid" do
      @s.sailyness = "0"
      @s.valid?.should == false
      @s.errors.full_messages.join(" ").should =~ /hehu!/
    end
  end
end