mezuro_range_spec.rb
2.6 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
77
78
79
80
81
82
83
require 'rails_helper'
describe MezuroRange, :type => :model do
subject { FactoryGirl.build(:mezuro_range, { metric_configuration_id: 42 }) }
describe 'validations' do
context 'active model validations' do
before :each do
BeginningUniquenessValidator.any_instance.stubs(:validate_each)
GreaterThanBeginningValidator.any_instance.stubs(:validate_each)
RangeOverlappingValidator.any_instance.stubs(:validate)
end
it { is_expected.to validate_presence_of(:beginning) }
it { is_expected.to validate_presence_of(:end) }
it { is_expected.to validate_presence_of(:reading_id) }
context 'beginning and end numericality' do
it { is_expected.to validate_numericality_of(:beginning) }
it { is_expected.to validate_numericality_of(:end) }
it 'should allow -INF and INF to beginning' do
subject.beginning = '-INF'
subject.save
expect(subject.errors.messages).to be_empty
subject.beginning = 'INF'
subject.save
expect(subject.errors.messages).to be_empty
end
it 'should allow -INF and INF to end' do
subject.end = '-INF'
subject.save
expect(subject.errors.messages).to be_empty
subject.end = 'INF'
subject.save
expect(subject.errors.messages).to be_empty
end
end
end
context 'beginning validations' do
before :each do
GreaterThanBeginningValidator.any_instance.stubs(:validate_each)
RangeOverlappingValidator.any_instance.stubs(:validate)
end
it 'should validate uniqueness' do
BeginningUniquenessValidator.any_instance.expects(:validate_each).with(subject, :beginning, subject.beginning)
subject.save
end
end
context 'end validations' do
before :each do
BeginningUniquenessValidator.any_instance.stubs(:validate_each)
RangeOverlappingValidator.any_instance.stubs(:validate)
end
it 'should validate that end is greater than beginning' do
GreaterThanBeginningValidator.any_instance.expects(:validate_each).with(subject, :end, subject.end)
subject.save
end
end
context 'overlapping validations' do
before :each do
GreaterThanBeginningValidator.any_instance.stubs(:validate_each)
BeginningUniquenessValidator.any_instance.stubs(:validate_each)
end
it 'is expected to validate if this range overlaps the existing ones' do
RangeOverlappingValidator.any_instance.expects(:validate).with(subject)
subject.save
end
end
end
end