/
ensure_exclusion_of_matcher_spec.rb
79 lines (64 loc) · 2.37 KB
/
ensure_exclusion_of_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
require 'spec_helper'
describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
context "an attribute which must be excluded of a range" do
before do
@model = define_model(:example, :attr => :integer) do
validates_exclusion_of :attr, :in => 2..5
end.new
end
it "should accept ensuring the correct range" do
@model.should ensure_exclusion_of(:attr).in_range(2..5)
end
it "should reject ensuring excluded value" do
@model.should_not ensure_exclusion_of(:attr).in_range(2..6)
end
it "should not override the default message with a blank" do
@model.should ensure_exclusion_of(:attr).in_range(2..5).with_message(nil)
end
end
context "an attribute with a custom ranged value validation" do
before do
@model = define_model(:example, :attr => :string) do
validates_exclusion_of :attr, :in => 2..4, :message => 'not good'
end.new
end
it "should accept ensuring the correct range" do
@model.should ensure_exclusion_of(:attr).in_range(2..4).with_message(/not good/)
end
end
context "an attribute with custom range validations" do
before do
define_model :example, :attr => :integer do
validate :custom_validation
def custom_validation
if attr >= 2 && attr <= 5
errors.add(:attr, 'shoud be out of this range')
end
end
end
@model = Example.new
end
it "should accept ensuring the correct range and messages" do
@model.should ensure_exclusion_of(:attr).in_range(2..5).with_message(/shoud be out of this range/)
end
end
context "an attribute which must be excluded in an array" do
before do
@model = define_model(:example, :attr => :string) do
validates_exclusion_of :attr, :in => %w(one two)
end.new
end
it "accepts with correct array" do
@model.should ensure_exclusion_of(:attr).in_array(%w(one two))
end
it "rejects when only part of array matches" do
@model.should_not ensure_exclusion_of(:attr).in_array(%w(one wrong_value))
end
it "rejects when array doesn't match at all" do
@model.should_not ensure_exclusion_of(:attr).in_array(%w(cat dog))
end
it "has correct description" do
ensure_exclusion_of(:attr).in_array([true, 'dog']).description.should == 'ensure exclusion of attr in [true, "dog"]'
end
end
end