# Rails functional test how to assert json result?

**URL:** <https://www.ruby-forum.com/t/rails-functional-test-how-to-assert-json-result/154754>\
**Category:** Rails\
**Created:** [December 3, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/rails-functional-test-how-to-assert-json-result/154754 "2008-12-03T16:15:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodwill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ac8455/32.png) [@goodwill](https://www.ruby-forum.com/u/goodwill)\
**Post date:** [December 3, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/rails-functional-test-how-to-assert-json-result/154754/1 "2008-12-03T16:15:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![goodwill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ac8455/32.png) [@goodwill](https://www.ruby-forum.com/u/goodwill)\
**Post date:** [December 3, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/rails-functional-test-how-to-assert-json-result/154754/2 "2008-12-03T20:47:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![goodwill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ac8455/32.png) [@goodwill](https://www.ruby-forum.com/u/goodwill)\
**Post date:** [December 4, 2008, 4:18am UTC](https://www.ruby-forum.com/t/rails-functional-test-how-to-assert-json-result/154754/3 "2008-12-04T04:18:51Z")

</div>

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