forked from jsonapi-rb/jsonapi-rails
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender_jsonapi_errors_spec.rb
More file actions
91 lines (77 loc) · 2.11 KB
/
Copy pathrender_jsonapi_errors_spec.rb
File metadata and controls
91 lines (77 loc) · 2.11 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'rails_helper'
describe ActionController::Base, '#render', type: :controller do
with_model :User, scope: :all do
table do |t|
t.string :name
t.string :email
end
model do
validates :name, presence: true
validates :email, format: { with: /@/, message: 'must be a valid email' }
end
end
let(:serialized_errors) do
{
'errors' => [
{
'detail' => "#{I18n.t('errors.messages.blank')}",
'title' => 'Invalid name',
'source' => { 'pointer' => '/data/attributes/name' }
},
{
'detail' => 'must be a valid email',
'title' => 'Invalid email',
'source' => { 'pointer' => '/data/attributes/email' }
}
],
'jsonapi' => { 'version' => '1.0' }
}
end
context 'when rendering ActiveModel::Errors' do
controller do
def create
user = User.new(email: 'lucas')
unless user.valid?
render jsonapi_errors: user.errors
end
end
def jsonapi_pointers
{
name: '/data/attributes/name',
email: '/data/attributes/email'
}
end
end
subject { JSON.parse(response.body) }
it 'renders a JSON API error document' do
post :create
expect(response.content_type).to eq('application/vnd.api+json')
is_expected.to eq(serialized_errors)
end
end
context 'when rendering error hashes' do
controller do
def create
errors = [
{
detail: "#{I18n.t('errors.messages.blank')}",
title: 'Invalid name',
source: { pointer: '/data/attributes/name' }
},
{
detail: 'must be a valid email',
title: 'Invalid email',
source: { pointer: '/data/attributes/email' }
}
]
render jsonapi_errors: errors
end
end
subject { JSON.parse(response.body) }
it 'renders a JSON API error document' do
post :create
expect(response.content_type).to eq('application/vnd.api+json')
is_expected.to eq(serialized_errors)
end
end
end