How to test JSON result from Ruby on Rails functional tests?

In Rails >= 5

Use ActionDispatch::TestResponse#parsed_body.

Example:

user = @response.parsed_body
assert_equal "Mike", user['name']

In Rails up to 4.x

Use JSON.parse, which takes a string as input and returns a Ruby hash that the JSON represents.

Example:

user = JSON.parse(@response.body)
assert_equal "Mike", user['name']

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)