How to test JSON result from Ruby on Rails functional tests?
How can I assert my Ajax request and test the JSON output from Ruby on Rails functional tests?
Use the JSON gem's JSON.parse, which takes a string as input and returns a Ruby hash that the JSON represents.
Here's the basic gist for a test:
user = JSON.parse(@response.body) assert_equal "Mike", user['name']
Rails has JSON support built in:
def json_response ActiveSupport::JSON.decode @response.body end
No need for a plugin
Then you can do something like this:
assert_equal "Mike", json_response['name']