Rails functional test how to assert json result?


#1

As title, I want to have my functional test check results from a json
render result, how can I achieve this?


#2

goodwill wrote:

As title, I want to have my functional test check results from a json
render result, how can I achieve this?

gem install assert_xpath

then in your test:

assert_xml @response.body
js = assert_xpath(’//path-to/your/javascript’).text
assert_javascript js
json = assert_xpath(’//path-to/your/json’)
hash = assert_json(json)
assert{ hash[:value] == 42 }

That is a long row to hoe just to get down to a 42. (You cannot unit
test the
result of rendering the json into a browser, either way.) So you could
stop
after the second line and just use:

assert{ js =~ /value: 42/ }


Phlip


#3

hmm… works, but obviously not a good looking one… thanks a lot
anyway. I would see if I could come up with something.