-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathnested_message_validator_spec.rb
More file actions
62 lines (48 loc) · 1.8 KB
/
nested_message_validator_spec.rb
File metadata and controls
62 lines (48 loc) · 1.8 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
require 'spec_helper'
require_relative '../../app/messages/nested_message_validator'
module VCAP::CloudController
class IncompleteValidator < NestedMessageValidator
end
class CompleteValidator < NestedMessageValidator
validates :invalid_message, presence: true, allow_nil: false
def invalid_message
nil
end
def should_validate?
true
end
def error_key
:data
end
end
class SampleActiveModel
include ActiveModel::Model
validates_with CompleteValidator
attr_accessor :data
end
RSpec.describe NestedMessageValidator do
context 'is an abstract interface' do
describe 'when it is subclassed' do
let(:incomplete_validator) { IncompleteValidator.new }
let(:complete_validator) { CompleteValidator.new }
let(:record) { SampleActiveModel.new }
it 'behaves like an ActiveModel::Validator' do
expect(complete_validator.is_a?(ActiveModel::Validator)).to be true
end
it 'must override should_validate?' do
expect { incomplete_validator.validate(record) }.to raise_error(/must declare when it should be run/)
expect { complete_validator.validate(record) }.not_to raise_error
end
it 'must override error_key' do
allow(incomplete_validator).to receive_messages(should_validate?: true, valid?: false)
expect { incomplete_validator.validate(record) }.to raise_error(/must declare where in record errors should be stored/)
expect { complete_validator.validate(record) }.not_to raise_error
end
it 'adds error messages at the specified key on the record that was validated' do
record.valid?
expect(record.errors[:data]).to include("Invalid message can't be blank")
end
end
end
end
end