|
1 | 1 | require 'spec_helper'
|
2 | 2 |
|
3 | 3 | RSpec.describe JsonapiErrorable::Serializers::Validation do
|
4 |
| - let(:errors_hash) { { username: ["can't be blank"] } } |
| 4 | + let(:attribute) { :username } |
| 5 | + let(:message) { "can't be blank" } |
| 6 | + let(:errors_hash) { { attribute => [message] } } |
5 | 7 |
|
6 | 8 | let(:object) { double(id: 123).as_null_object }
|
7 | 9 | let(:instance) { described_class.new(object) }
|
|
12 | 14 | .to receive(:reflect_on_all_associations)
|
13 | 15 | .and_return([double(name: :pets)])
|
14 | 16 | allow(object).to receive_message_chain(:errors, :to_hash) { errors_hash }
|
| 17 | + allow(object).to receive_message_chain(:errors, :full_message).with(attribute, message) { "#{attribute.capitalize} #{message}" } |
15 | 18 | end
|
16 | 19 |
|
17 | 20 | describe '#errors' do
|
|
23 | 26 |
|
24 | 27 | context 'when the error is on an attribute' do
|
25 | 28 | before do
|
26 |
| - allow(object).to receive(:respond_to?).with(:username) { true } |
| 29 | + allow(object).to receive(:respond_to?).with(attribute) { true } |
27 | 30 | end
|
28 | 31 |
|
29 | 32 | it 'renders valid JSONAPI error format' do
|
|
45 | 48 | end
|
46 | 49 |
|
47 | 50 | context 'when the error attribute is "base"' do
|
48 |
| - let(:errors_hash) { { base: ["Model is invalid"] } } |
| 51 | + let(:attribute) { :base } |
| 52 | + let(:message) { "Model is invalid" } |
49 | 53 |
|
50 | 54 | before do
|
51 |
| - allow(object).to receive(:respond_to?).with(:base) { true } |
| 55 | + allow(object).to receive(:respond_to?).with(attribute) { true } |
52 | 56 | end
|
53 | 57 |
|
54 | 58 | it 'should not render the attribute in the message detail' do
|
|
72 | 76 | end
|
73 | 77 |
|
74 | 78 | context 'when the error is on a relationship' do
|
75 |
| - let(:errors_hash) { { pets: ["is invalid"] } } |
| 79 | + let(:attribute) { :pets } |
| 80 | + let(:message) { "is invalid" } |
76 | 81 |
|
77 | 82 | it 'puts the source pointer on relationships' do
|
78 | 83 | expect(subject).to eq(
|
|
92 | 97 | context 'but the object is not activerecord' do
|
93 | 98 | before do
|
94 | 99 | allow(instance).to receive(:activemodel?) { false }
|
95 |
| - allow(object).to receive(:respond_to?).with(:pets) { true } |
| 100 | + allow(object).to receive(:respond_to?).with(attribute) { true } |
96 | 101 | end
|
97 | 102 |
|
98 | 103 | it 'places the error on attribute' do
|
|
112 | 117 |
|
113 | 118 | context 'but the object does not respond to this property' do
|
114 | 119 | before do
|
115 |
| - allow(object).to receive(:respond_to?).with(:pets) { false } |
| 120 | + allow(object).to receive(:respond_to?).with(attribute) { false } |
116 | 121 | end
|
117 | 122 |
|
118 | 123 | it 'defaults to relationship' do
|
|
134 | 139 | end
|
135 | 140 |
|
136 | 141 | context 'when the error is neither a relationship or attribute of the object' do
|
137 |
| - let(:errors_hash) { { :'foo.bar' => ["is invalid"] } } |
| 142 | + let(:attribute) { :'foo.bar' } |
| 143 | + let(:message) { "is invalid" } |
138 | 144 |
|
139 | 145 | before do
|
140 |
| - allow(object).to receive(:respond_to?).with(:'foo.bar') { false } |
| 146 | + allow(object).to receive(:respond_to?).with(attribute) { false } |
141 | 147 | end
|
142 | 148 |
|
143 | 149 | it 'puts the source pointer on relationships' do
|
|
0 commit comments