-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
Add response.parsed_body
to Integration Tests
#23597
Conversation
When testing: ```ruby post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json ``` It's common to want to make assertions on the response body. Perhaps the server responded with JSON, so you write `JSON.parse(response.body)`. But that gets tedious real quick. Instead add `parsed_body` which will automatically parse the reponse body as what the last request was encoded `as`.
Forgot to add this in the original pull request. No biggie, just show some examples.
d31dc69
to
c85b177
Compare
Passed on this earlier build, https://travis-ci.org/rails/rails/builds/108383081, before I force pushed some changelog changes. |
Add `response.parsed_body` to Integration Tests
@@ -422,17 +424,21 @@ def encode_params(params) | |||
@param_encoder.call(params) | |||
end | |||
|
|||
def parse_body(body) | |||
@response_parser.call(body) | |||
end |
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.
Would be nice if this was memoized. Frequent to have a bunch of checks against response.parsed_body['key1'], response.parsed_body['key2'].
We should extend this to work for the get case as well and whenever format is set: get circle_people_url(format: :json)
assert_equal "something", response.parsed_body['key'] Right now it doesn't as format isn't used to detect the mime. |
class ApiTest < ActionDispatch::IntegrationTest | ||
test 'creates articles' do | ||
assert_difference -> { Article.count } do | ||
post articles_path, { article: { title: 'Ahoy!' } }, as: :json |
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 should be: post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json
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.
Doh! 😁
@dhh ah, you'd like us to replace |
I'd like for response.parsed_body to work if the response is encoded as On Thu, Feb 11, 2016 at 2:00 PM, Kasper Timm Hansen <
|
Yeah, this shouldn't need to be based on the request encoding at all: the response headers tell us whether the response is JSON. |
@matthewd ah, of course 👍. Will patch this up tonight. |
@dhh @matthewd fixed your suggestions here f9f98b7...036a7a0 |
Super!
|
Follow up to rails#23597
@dhh wups! Just noticed this doesn't work with |
I noticed this still doesn't work with RSpec unit testing controllers for v5.2.4.3: RSpec.describe Api::MyApi, type: :controller do
controller do
def create
render json: { foo: 'bar' }
end
end
it do
# trying to do all forms of informing the testing framework that we're a JSON request
@request.headers['Accept'] = 'application/json'
@request.headers['Content-Type'] = 'application/json'
post :create, as: :json, format: :json
expect(response.parsed_body).to eq( 'foo' => 'bar' ) # fails
end
end Inspecting the code, it looks like this is because the As far as I can see, additional work is required to change the |
Follow up to #21671
Fixes #23594