Skip to content

Commit 6250d87

Browse files
authored
Merge pull request #10 from happycollision/fix-issue-9
Fix issue #9
2 parents 3292476 + 9c32ac8 commit 6250d87

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/jsonapi_spec_helpers/helpers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def json_ids(integers = false)
5656

5757
def validation_errors
5858
@validation_errors ||= {}.tap do |errors|
59+
return errors if json['errors'].nil?
5960
json['errors'].each do |e|
6061
attr = e['meta']['attribute'].to_sym
6162
message = e['meta']['message']

spec/helpers_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@
7070
}
7171
end
7272

73+
let(:errors_json) do
74+
{
75+
'errors' => [
76+
{
77+
'code' => 'unprocessable_entity',
78+
'status' => '422',
79+
'title' => 'Validation Error',
80+
'detail' => 'Patron must exist',
81+
'source' => {
82+
'pointer' => '/data/relationships/patron'
83+
},
84+
'meta' => {
85+
'attribute' => 'patron',
86+
'message' => 'must exist',
87+
'code' => 'blank'
88+
}
89+
}
90+
]
91+
}
92+
end
93+
7394
describe '#json_item' do
7495
let(:json) { show_json }
7596

@@ -166,4 +187,21 @@
166187
})
167188
end
168189
end
190+
191+
describe '#validation_errors' do
192+
let(:json) { errors_json }
193+
194+
it 'creates a hash of the errors' do
195+
expect(validation_errors).to eq({:patron => 'must exist'})
196+
end
197+
198+
describe 'when there are no errors' do
199+
let(:json) { show_json }
200+
it 'does not raise an error of its own' do
201+
expect{ validation_errors }.not_to raise_error
202+
expect(validation_errors[:any_key_here]).to be_nil
203+
end
204+
end
205+
end
206+
169207
end

0 commit comments

Comments
 (0)