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.
6856d20b5d6cbdb827cdccca7f557979?d=identicon&s=25 William Yeung (goodwill)
on 2008-12-03 16: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?
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-12-03 20: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
6856d20b5d6cbdb827cdccca7f557979?d=identicon&s=25 William Yeung (goodwill)
on 2008-12-04 04: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.