-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Updated isolated tests to assert correct behavior. #2010
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
Changes from 3 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,8 @@ class << self | |
end | ||
|
||
def render_with_jsonapi_renderer | ||
author = Author.new(params[:data][:attributes]) | ||
attributes = params[:data].present? ? params[:data][:attributes] : {} | ||
author = Author.new(attributes) | ||
render jsonapi: author | ||
end | ||
|
||
|
@@ -64,13 +65,13 @@ def test_jsonapi_renderer_not_registered | |
'attributes' => { | ||
'name' => 'Johnny Rico' | ||
}, | ||
'type' => 'users' | ||
'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. why? type looks correct to me 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 just wanted it to match what was being returned from controller. I don't have an issue with changing it back to "users" just thought that this way it would match. |
||
} | ||
} | ||
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}' | ||
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' } | ||
post '/render_with_jsonapi_renderer', params: payload, headers: headers | ||
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. so, this should have raised an exception when hitting 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. the problem is that it raises an exception while trying to create a new author since params are not parsed correctly without this. So, I can setup another api method. I think i have a way to change controller to test this case. Let me take a shot. 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. @bf4 i can't get the test controller to return the actual exception. I tried using rescue_from but i think since the error is with render it just returns 500 with blank body. I think for now, I am just going to assert that response.status = 500 |
||
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! |
||
end | ||
|
||
def test_jsonapi_parser | ||
|
@@ -111,18 +112,12 @@ def test_jsonapi_parser_registered | |
end | ||
|
||
def test_jsonapi_renderer_registered | ||
expected = { | ||
'data' => { | ||
'attributes' => { | ||
'name' => 'Johnny Rico' | ||
}, | ||
'type' => 'users' | ||
} | ||
} | ||
expected = { 'name' => 'Johnny Rico' } | ||
|
||
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, JSON.parse(response.body)['data']['attributes'] | ||
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. Why only checking part of response. did this fail when changing 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. it did fail. The problem is that response.body contains a lot of stuff then just data.attributes and type. It includes relationships, id and other things. 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. here is the actual response.body {
"data": {
"id": "author",
"type": "authors",
"attributes": {
"name": "Johnny Rico"
},
"relationships": {
"posts": {
"data": null
},
"roles": {
"data": null
},
"bio": {
"data": null
}
}
}
} 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. Oh, this is because of the PORO Fix is we should be explicit what our model and serializer are: class JsonApiRendererTest < ActionDispatch::IntegrationTest
include ActiveSupport::Testing::Isolation
class Author < ::Model
attributes :name
end
class AuthorSerializer < ActiveModel::Serializer
type: 'users'
attribute :id
attribute :name
end I think that's a better expectation, no? 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 think so. But I can't get your code to work. The dependency required is getting a bit too much. For now, i have updated expected object to include all relationships so that we check for exact output. |
||
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.
why? how could this not be set and why would we want that fallback if not set?
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.
this is because when adapter is not set as jsonapi, params are not set. This is there so that it fails on render and not on trying to set Author object. Since we use the same controller in case where it should succeed and fail, I wanted to make sure this part doesn't cause a failure.