Skip to content

Commit f246741

Browse files
akshah123bf4
authored andcommitted
Updated isolated tests to assert correct behavior. (#2010)
* Updated isolated tests to assert correct behavior. * Added check to get unsafe params if rails version is great than 5
1 parent 4e43013 commit f246741

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class << self
1212
end
1313

1414
def render_with_jsonapi_renderer
15-
author = Author.new(params[:data][:attributes])
15+
unlocked_params = Rails::VERSION::MAJOR >= 5 ? params.to_unsafe_h : params
16+
attributes = unlocked_params[:data].present? ? unlocked_params[:data][:attributes] : {}
17+
author = Author.new(attributes)
1618
render jsonapi: author
1719
end
1820

@@ -59,18 +61,12 @@ def test_jsonapi_parser_not_registered
5961
end
6062

6163
def test_jsonapi_renderer_not_registered
62-
expected = {
63-
'data' => {
64-
'attributes' => {
65-
'name' => 'Johnny Rico'
66-
},
67-
'type' => 'users'
68-
}
69-
}
7064
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
7165
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
7266
post '/render_with_jsonapi_renderer', params: payload, headers: headers
73-
assert expected, response.body
67+
assert_equal 500, response.status
68+
assert_equal '', response.body
69+
assert response.request.env['action_dispatch.exception'].is_a?(ActionView::MissingTemplate) if response.request.present?
7470
end
7571

7672
def test_jsonapi_parser
@@ -113,16 +109,21 @@ def test_jsonapi_parser_registered
113109
def test_jsonapi_renderer_registered
114110
expected = {
115111
'data' => {
116-
'attributes' => {
117-
'name' => 'Johnny Rico'
118-
},
119-
'type' => 'users'
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+
}
120120
}
121121
}
122+
122123
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
123124
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
124125
post '/render_with_jsonapi_renderer', params: payload, headers: headers
125-
assert expected, response.body
126+
assert_equal expected.to_json, response.body
126127
end
127128

128129
def test_jsonapi_parser

0 commit comments

Comments
 (0)