Skip to content

Commit 11c4efb

Browse files
committed
Cleanup assertions in isolated jsonapi renderer tests a bit
1 parent f246741 commit 11c4efb

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class << self
1313

1414
def render_with_jsonapi_renderer
1515
unlocked_params = Rails::VERSION::MAJOR >= 5 ? params.to_unsafe_h : params
16-
attributes = unlocked_params[:data].present? ? unlocked_params[:data][:attributes] : {}
16+
attributes = unlocked_params[:data].present? ? unlocked_params[:data][:attributes].merge(id: unlocked_params[:data][:id]) : {}
1717
author = Author.new(attributes)
1818
render jsonapi: author
1919
end
@@ -34,6 +34,17 @@ def assert_parses(expected, actual, headers = {})
3434
assert_equal(expected, TestController.last_request_parameters)
3535
end
3636

37+
def define_author_model_and_serializer
38+
TestController.const_set(:Author, Class.new(ActiveModelSerializers::Model) do
39+
attributes :name
40+
end)
41+
TestController.const_set(:AuthorSerializer, Class.new(ActiveModel::Serializer) do
42+
type 'users'
43+
attribute :id
44+
attribute :name
45+
end)
46+
end
47+
3748
class WithoutRenderer < JsonApiRendererTest
3849
setup do
3950
require 'rails'
@@ -49,6 +60,7 @@ class WithoutRenderer < JsonApiRendererTest
4960
match ':action', to: TestController, via: [:get, :post]
5061
end
5162
end
63+
define_author_model_and_serializer
5264
end
5365

5466
def test_jsonapi_parser_not_registered
@@ -61,12 +73,12 @@ def test_jsonapi_parser_not_registered
6173
end
6274

6375
def test_jsonapi_renderer_not_registered
64-
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
76+
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
6577
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
6678
post '/render_with_jsonapi_renderer', params: payload, headers: headers
67-
assert_equal 500, response.status
6879
assert_equal '', response.body
69-
assert response.request.env['action_dispatch.exception'].is_a?(ActionView::MissingTemplate) if response.request.present?
80+
assert_equal 500, response.status
81+
assert_equal ActionView::MissingTemplate, request.env['action_dispatch.exception'].class
7082
end
7183

7284
def test_jsonapi_parser
@@ -94,6 +106,7 @@ class WithRenderer < JsonApiRendererTest
94106
match ':action', to: TestController, via: [:get, :post]
95107
end
96108
end
109+
define_author_model_and_serializer
97110
end
98111

99112
def test_jsonapi_parser_registered
@@ -109,18 +122,13 @@ def test_jsonapi_parser_registered
109122
def test_jsonapi_renderer_registered
110123
expected = {
111124
'data' => {
112-
'id' => 'author',
113-
'type' => 'authors',
114-
'attributes' => { 'name' => 'Johnny Rico' },
115-
'relationships' => {
116-
'posts' => { 'data' => nil },
117-
'roles' => { 'data' => nil },
118-
'bio' => { 'data' => nil }
119-
}
125+
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765',
126+
'type' => 'users',
127+
'attributes' => { 'name' => 'Johnny Rico' }
120128
}
121129
}
122130

123-
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
131+
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
124132
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
125133
post '/render_with_jsonapi_renderer', params: payload, headers: headers
126134
assert_equal expected.to_json, response.body
@@ -133,10 +141,11 @@ def test_jsonapi_parser
133141
'attributes' => {
134142
'name' => 'John Doe'
135143
},
136-
'type' => 'users'
144+
'type' => 'users',
145+
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765'
137146
}
138147
},
139-
'{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}',
148+
'{"data": {"attributes": {"name": "John Doe"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}',
140149
'CONTENT_TYPE' => 'application/vnd.api+json'
141150
)
142151
end

0 commit comments

Comments
 (0)