Read more

Rails: How to test the parsed response body

Emanuel
April 12, 2023Software engineer at makandra GmbH

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'}, ...]
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

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.

Posted by Emanuel to makandra dev (2023-04-12 14:12)