Rails: How to test the parsed response body

Testing your responses in Rails allows to parse the body depending on the response MIME type Show archive.org snapshot with parsed_body.

get '/posts.json'
response.parsed_body # => [{'id' => 42,  'title' => 'Title'}, ...]

For JSON APIs we often parse the response as symbolized keys with JSON.parse(response.body, symbolize_names: true), which is not supported by parsed_body. For all other cases you might want to drop JSON.parse(response.body) and replace it with parsed_body.

There also exists a cop Rails/ResponseParsedBody Show archive.org snapshot that you can enable via rubocop-rails.

Emanuel About 1 year ago