Testing JSON/XML responses in Rails

rails test spec

Mon Dec 14 20:38:10 +0000 2009

Recently I had to find out how to test the output of a rails controller when the json format is specified. I’m using rspec by the way, but this should work in other frameworks as well.

My instinct, which was wrong, was to do what we do all over the rest of rails:

get :index, :format => :json

But this is not correct. The right way is to do this:

request.accept = 'application/json'
get :index

Do that, and everything works great.

blog comments powered by Disqus