Rails: How to test the parsed response body

Posted . Visible to the public.

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.

Last edit
Emanuel
License
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2023-04-12 12:12)