/
validate_exact_length_matcher_spec.rb
90 lines (82 loc) · 3.49 KB
/
validate_exact_length_matcher_spec.rb
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require File.dirname(__FILE__) + "/spec_helper"
describe "validate_exact_length_matcher" do
before :all do
class Item < Sequel::Model
plugin :validation_helpers
def validate
validates_exact_length 4, :name, :allow_nil => true
end
end
end
after :all do
Object.send(:remove_const, :Item)
end
subject{ Item }
describe "arguments" do
it "should require attribute" do
lambda{
@matcher = validate_exact_length
}.should raise_error(ArgumentError)
end
it "should require additionnal parameters" do
lambda{
@matcher = validate_exact_length :name
}.should raise_error(ArgumentError)
end
it "should refuse invalid additionnal parameters" do
lambda{
@matcher = validate_exact_length :id, :name
}.should raise_error(ArgumentError)
end
it "should accept valid additionnal parameters" do
lambda{
@matcher = validate_exact_length 4, :name
}.should_not raise_error(ArgumentError)
end
end
describe "messages" do
describe "without option" do
it "should contain a description" do
@matcher = validate_exact_length 4, :name
@matcher.description.should == "validate exact length of :name to 4"
end
it "should set failure messages" do
@matcher = validate_exact_length 4, :name
@matcher.matches? subject
@matcher.failure_message.should == "expected Item to validate exact length of :name to 4"
@matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate")
end
end
describe "with options" do
it "should contain a description" do
@matcher = validate_exact_length 4, :name, :allow_nil => true
@matcher.description.should == "validate exact length of :name to 4 with :allow_nil => true"
end
it "should set failure messages" do
@matcher = validate_exact_length 4, :price, :allow_nil => true
@matcher.matches? subject
@matcher.failure_message.should == "expected Item to validate exact length of :price to 4 with :allow_nil => true"
@matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate")
end
it "should explicit used options if different than expected" do
@matcher = validate_exact_length 4, :name, :allow_blank => true
@matcher.matches? subject
@matcher.failure_message.should == "expected Item to validate exact length of :name to 4 with :allow_blank => true but called with option(s) :allow_nil => true instead"
@matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate")
end
it "should warn if invalid options are used" do
@matcher = validate_exact_length 4, :name, :allow_anything => true
@matcher.matches? subject
@matcher.failure_message.should == "expected Item to validate exact length of :name to 4 with :allow_anything => true but option :allow_anything is not valid"
@matcher.negative_failure_message.should == @matcher.failure_message.gsub("to validate", "to not validate")
end
end
end
describe "matchers" do
it{ should validate_exact_length(4, :name) }
it{ should validate_exact_length(4, :name, :allow_nil => true) }
it{ should_not validate_exact_length(4, :price) }
it{ should_not validate_exact_length(3, :name) }
it{ should_not validate_exact_length(4, :name, :allow_blank => true) }
end
end