Forum: Ruby on Rails Rails functional test how to assert json result?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
William Y. (Guest)
on 2008-12-03 17:15
(Received via mailing list)
As title, I want to have my functional test check results from a json
render result, how can I achieve this?
Phlip (Guest)
on 2008-12-03 21:47
(Received via mailing list)
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
William Y. (Guest)
on 2008-12-04 05:18
(Received via mailing list)
hmm... works, but obviously not a good looking one... thanks a lot
anyway. I would see if I could come up with something.
This topic is locked and can not be replied to.