Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/jsonapi_spec_helpers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def json_ids(integers = false)

def validation_errors
@validation_errors ||= {}.tap do |errors|
return errors if json['errors'].nil?
json['errors'].each do |e|
attr = e['meta']['attribute'].to_sym
message = e['meta']['message']
Expand Down
38 changes: 38 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@
}
end

let(:errors_json) do
{
'errors' => [
{
'code' => 'unprocessable_entity',
'status' => '422',
'title' => 'Validation Error',
'detail' => 'Patron must exist',
'source' => {
'pointer' => '/data/relationships/patron'
},
'meta' => {
'attribute' => 'patron',
'message' => 'must exist',
'code' => 'blank'
}
}
]
}
end

describe '#json_item' do
let(:json) { show_json }

Expand Down Expand Up @@ -166,4 +187,21 @@
})
end
end

describe '#validation_errors' do
let(:json) { errors_json }

it 'creates a hash of the errors' do
expect(validation_errors).to eq({:patron => 'must exist'})
end

describe 'when there are no errors' do
let(:json) { show_json }
it 'does not raise an error of its own' do
expect{ validation_errors }.not_to raise_error
expect(validation_errors[:any_key_here]).to be_nil
end
end
end

end