# Testing JSON stuff

**URL:** <https://www.ruby-forum.com/t/testing-json-stuff/114822>\
**Category:** Rails\
**Created:** [September 17, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/testing-json-stuff/114822 "2007-09-17T21:00:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 17, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/testing-json-stuff/114822/1 "2007-09-17T21:00:19Z")

</div>

Hi,

I have some code in a controller that receives JSON requests in this  
sort of format:

[http://www.json.org/JSONRequest.html](http://www.json.org/JSONRequest.html)

However, I am not finding a way to test that, since:

1. post() seems to insist on sending key-value pairs.

2. I’m not sure what to post to trigger the format.js in respond\_to

Any suggestions?

Thankyou,  
Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 18, 2007, 7:39am UTC](https://www.ruby-forum.com/t/testing-json-stuff/114822/2 "2007-09-18T07:39:54Z")

</div>

First of all, json is usually only sent to javascript apps on web  
browsers do to lack of standard xml support. Maybe you should consider  
sending xml to your rails app.

Secondly, I think you mean something like this:

respond\_to do |format|  
format.xml { render :xml =\> @your\_object.to\_xml }  
format.json { render :json =\> @your\_object.to\_json }  
format.yaml { render :yaml =\> @your\_object.to\_yaml }  
end

On Sep 17, 11:59 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 18, 2007, 10:21am UTC](https://www.ruby-forum.com/t/testing-json-stuff/114822/3 "2007-09-18T10:21:15Z")

</div>

> First of all, json is usually only sent to javascript apps on web  
> browsers do to lack of standard xml support. Maybe you should consider  
> sending xml to your rails app.

Hi, thanks - it’s not a bad idea, but we need to handle json in our  
rails app, and I want to be able to test it, of course.

> Secondly, I think you mean something like this:
> 
> respond\_to do |format|  
> format.xml { render :xml =\> @your\_object.to\_xml }  
> format.json { render :json =\> @your\_object.to\_json }

Oops, of course. Doesn’t fix the problem though, I still get a test  
failure when doing:

```
post(:create, {'foo' => 'bar'}, {"Content-Type" => "application/

```

json"})  
assert\_equal @response.body, “{good: “ok”}”

against

```
  format.json {
    ok = { :good => 'ok' }
    render :json => ok.to_json
  }

```

> ```
> format.yaml { render :yaml => @your_object.to_yaml }
> end
> 
> ```

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 18, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/testing-json-stuff/114822/4 "2007-09-18T14:57:07Z")

</div>

Ok… I think I figured it out. The problem is that I needed an  
integration test, not a functional test. The way the two work seems to  
be subtly different. This works ok:

def test\_create  
open\_session do |sess|  
sess.post(’/resource/create.js’, ‘{answer: 42}’, {“Content-type”  
=\> “application/json”})  
assert\_equal “{answerplusone: 43}”, sess.response.body  
end  
end

With this:

def create  
respond\_to do |format|  
format.html {  
render :text =\> ‘hello world’  
}  
format.js {  
ok = { :good =\> ‘ok’ }  
render :json =\> ok.to\_json  
}  
end  
end

Well… at least in the sense of sending the right stuff and getting  
the right section of code back.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 18, 2007, 12:25pm UTC](https://www.ruby-forum.com/t/testing-json-stuff/114822/5 "2007-09-18T12:25:37Z")

</div>

Progress… sort of. But I’m even more suspicious of this testing  
framework now:

```
post(:create, {:q => 'foo'}, {"Content-Type" => "application/

```

json"})  
assert\_equal “{good: “ok”}”, @response.body

However, in the controller, I have:

```
logger.warn "INFO: " + request.env.inspect
logger.warn "HEADERS: " + headers.inspect

```

And I get:

INFO: {“REMOTE\_ADDR”=\>“0.0.0.0”, “REQUEST\_URI”=\>"/resource/create?  
q=foo", “SERVER\_PORT”=\>80, “REQUEST\_METHOD”=\>“POST”}  
HEADERS: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

1. The URI should not include q=foo - those should be passed as part  
of the POST, no?

2. What the heck happened to my Content-Type header?!

This isn’t making much sense at all.

Thanks,  
Dave
