-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated isolated tests to assert correct behavior. #2010
Changes from all commits
8d18374
921ffac
baf58c7
57a4fc2
2debd58
b712de3
377ea05
c4a27b2
822e1b2
ffde106
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ class << self | |
end | ||
|
||
def render_with_jsonapi_renderer | ||
author = Author.new(params[:data][:attributes]) | ||
unlocked_params = Rails::VERSION::MAJOR >= 5 ? params.to_unsafe_h : params | ||
attributes = unlocked_params[:data].present? ? unlocked_params[:data][:attributes] : {} | ||
author = Author.new(attributes) | ||
render jsonapi: author | ||
end | ||
|
||
|
@@ -59,18 +61,12 @@ def test_jsonapi_parser_not_registered | |
end | ||
|
||
def test_jsonapi_renderer_not_registered | ||
expected = { | ||
'data' => { | ||
'attributes' => { | ||
'name' => 'Johnny Rico' | ||
}, | ||
'type' => 'users' | ||
} | ||
} | ||
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}' | ||
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' } | ||
post '/render_with_jsonapi_renderer', params: payload, headers: headers | ||
assert expected, response.body | ||
assert_equal 500, response.status | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we make an assertion about the nature of the failure by matching on the response.body? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't get response.body to show the exception. It kept returning empty body. I think this probably has something to do with it failing on render. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good. can you add an assertion for the empty body? If you know what the exception actually, was, would love for you to paste it and stack trace here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actual exception message:
backtrace
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. awesome! thanks! |
||
assert_equal '', response.body | ||
assert response.request.env['action_dispatch.exception'].is_a?(ActionView::MissingTemplate) if response.request.present? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I can tell, it changes on a specific rails version. At some 4.x version, we start getting response.request but we dont' have that for 4.1. |
||
end | ||
|
||
def test_jsonapi_parser | ||
|
@@ -113,16 +109,21 @@ def test_jsonapi_parser_registered | |
def test_jsonapi_renderer_registered | ||
expected = { | ||
'data' => { | ||
'attributes' => { | ||
'name' => 'Johnny Rico' | ||
}, | ||
'type' => 'users' | ||
'id' => 'author', | ||
'type' => 'authors', | ||
'attributes' => { 'name' => 'Johnny Rico' }, | ||
'relationships' => { | ||
'posts' => { 'data' => nil }, | ||
'roles' => { 'data' => nil }, | ||
'bio' => { 'data' => nil } | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
|
||
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we should include a uuid here. how about You've been working on this PR a while now, and the main goal of it is done. Thanks! I can do some of the other things I'd like in a followup :) |
||
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' } | ||
post '/render_with_jsonapi_renderer', params: payload, headers: headers | ||
assert expected, response.body | ||
assert_equal expected.to_json, response.body | ||
end | ||
|
||
def test_jsonapi_parser | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I gather
Author.new(params[:data][:attributes])
is failing for some reason. perhaps we should insteadauthor = Author.new(params.require(:data).require(:attributes))
? I'm not sure what the need is to cast the ActionController::Parameters or whendata
wouldn't be present.In actual usage I would use the JsonApi deserializer here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right that params[:data] is not set at all and the reason is that app doesn't know how to parse provided data.
.require
will not help since it doesn't see data at all. Also, require will cause problem when jsonapi is not registered since it doesn't see the actual value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, so params is
{}
? Then that's probably still a good failure :)