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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)