/
validation_serialization_spec.rb
78 lines (64 loc) · 2.01 KB
/
validation_serialization_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
# frozen_string_literal: true
require 'spec_helper'
describe ZendeskAppsSupport::Validations::ValidationError do
ValidationError = ZendeskAppsSupport::Validations::ValidationError
it 'symbolizes the keys in its data' do
error = ValidationError.new(:foo, 'bar' => 'baz')
expect(error.data[:bar]).to eq('baz')
end
describe '#to_json' do
let(:key) { 'foo.bar' }
let(:data) { { 'baz' => 'quux' } }
let(:error) { ValidationError.new(key, data) }
subject { JSON.generate(error) }
it do
is_expected.to eq(JSON.generate(
'class' => error.class.to_s,
'key' => error.key,
'data' => error.data
))
end
end
describe '.from_hash' do
subject { ValidationError.from_hash(hash) }
context 'for a generic error' do
let(:hash) do
{
'class' => 'ZendeskAppsSupport::Validations::ValidationError',
'key' => 'foo.bar.baz',
'data' => { 'quux' => 'yargle' }
}
end
it { is_expected.to be_a(ValidationError) }
describe '#key' do
subject { super().key }
it { is_expected.to eq('foo.bar.baz') }
end
describe '#data' do
subject { super().data }
it { is_expected.to eq(quux: 'yargle') }
end
end
context 'for a non-ValidationError hash' do
let(:hash) do
{
foo: 'bar'
}
end
it 'raises a DeserializationError' do
expect { subject }.to raise_error(ValidationError::DeserializationError)
end
end
end
describe '.from_json' do
it 'decodes a JSON hash and passes it to .from_hash' do
expect(ValidationError).to receive(:from_hash).with({ 'foo' => 'bar' })
ValidationError.from_json(JSON.generate('foo' => 'bar'))
end
it 'raises a DeserializationError when passed non-JSON' do
expect do
ValidationError.from_json('}}}')
end.to raise_error(ValidationError::DeserializationError)
end
end
end