My approach to this is slightly different if I'm using the Jbuilder
gem that is now available from the Rails team. (This approach applies to other gems that render JSON or XML as views.) I prefer unit tests over functional tests whenever possible, since they can be quite a bit faster. With Jbuilder you can transform most of the tests into unit tests.
Yes, you still have functional tests on the controller, but there are very few and they don't parse the JSON. The functional test solely test the controller logic, not the rendered JSON. A functional test for a valid request might assert the following (RSpec):
assert_response :success
expect(response).to render_template(:show)
expect(assigns(:item).id).to eq(expected_item.id)
I'm just verifying that it is successful, it renders the template, and it passes the item to the template. At this point, the view has the information it needs to do the proper rendering.
Now test the JSON rendered by unit testing the Jbuilder view.
describe 'api/v1/items/show.json.jbuilder' do
it 'includes foo' do
assign(:item, account.build(foo: 'bar'))
render
json = JSON.parse(rendered)
expect(json['item']['foo']).to eq('bar')
end
# A bunch of other JSON tests...