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

William Yeung picture William Yeung · Dec 3, 2008 · Viewed 36.8k times · Source

How can I assert my Ajax request and test the JSON output from Ruby on Rails functional tests?

Answer

Josh picture Josh · Aug 20, 2010

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']