Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 93 lines (76 sloc) 3.116 kb
ded4337 @boof Initial Import.
authored
1 require "#{ File.dirname __FILE__ }/sequel_spec_helper.rb"
2
3 describe Sequel::Plugins::NotNaughty do
4
5 before(:each) do
6 @obj = Class.new(Sequel::Model) { is :notnaughty }
7 end
8
9 it "should delegate validation methods in receiver" do
10 @instance = @obj.new
11
12 @obj.validator.should be_an_instance_of(Sequel::Plugins::NotNaughty)
13 @obj.validator.should_receive(:states).and_return({})
14 @obj.validator.should_receive(:has_validations?)
15 @obj.validator.should_receive(:invoke).with(@instance)
16
17 @instance.errors.should be_an_instance_of(subject::Violation)
18 @instance.should respond_to(:validate)
19 @instance.should respond_to(:valid?)
20
21 @obj.validations
22 @obj.has_validations?
23 @obj.validate @instance
24 end
25 it "should return validations" do
26 @obj.validations.should be_an_instance_of(Hash)
27 end
28 it "should not have validations" do
29 @obj.has_validations?.should == false
30 end
31 it "should add_validation instance of validation" do
32 validation = Class.new(subject::Validation)
33
34 @obj.validator.add_validation validation, :attribute
35
36 validations = @obj.validator.states[:create][:attribute]
37 validations.length.should == 1
38 validations.first.should be_an_instance_of(validation)
39
40 validations = @obj.validator.states[:update][:attribute]
41 validations.length.should == 1
42 validations.first.should be_an_instance_of(validation)
43
44 @obj.validator.add_validation validation, :attribute, :on => :create
45
46 validations = @obj.validator.states[:create][:attribute]
47 validations.length.should == 2
48 validations[0].should be_an_instance_of(validation)
49 validations[1].should be_an_instance_of(validation)
50
51 validations = @obj.validator.states[:update][:attribute]
52 validations.length.should == 1
53 validations.first.should be_an_instance_of(validation)
54 end
55 it "should have validations" do
56 validation = Class.new(subject::Validation)
57 @obj.validator.add_validation validation, :attribute
58
59 @obj.has_validations?.should == true
60 end
61 it "should add_validation blocks as Validation" do
62 @obj.validator.add_validation(:attribute) { |o, a, v| }
63
64 @obj.validator.states[:create][:attribute].first.
65 should be_kind_of(subject::Validation)
66 @obj.validator.states[:update][:attribute].first.
67 should be_kind_of(subject::Validation)
68 end
69 it "should run validations on object when it's invoked" do
70 probe = mock 'Probe', :new? => true
71 probe.should_receive(:attribute).and_return(1)
72 probe.should_receive(:test).with(:attribute, 1)
73
74 @obj.validator.add_validation(:attribute) { |o, a, v| o.test a, v }
75 @obj.validate probe
76 end
77 it "should validate if saved" do
78 x = rand
79 instance = @obj.new
80 instance.stub!(:save!).and_return(x)
81 @obj.validator.should_receive(:invoke).with(instance)
82 instance.save.should be(x)
83 end
84 it "should run hooks if validated" do
85 instance = @obj.new
86 instance.should_receive(:before_validation)
87 instance.should_receive(:after_validation)
88 instance.stub!(:save!)
89 instance.save
90 end
91
92 end
Something went wrong with that request. Please try again.