timpone
timpone

Reputation: 19979

using rspec controllers to test JSON API

I am writing some tests for a JSON API. Would using controller specs with render_views be an acceptable way of doing this? For example, something like:

describe 'task028: Adding an Item via MenuHeader', task028: true do
    it 'should create a new menu item' do
      m=FactoryGirl.create(:menu)
      mh=FactoryGirl.create(:header, parent: nil, menu_id: m.id)
      Item.count.should eq(0)
      post :create,  {item: { header: "my menu item header", detail: "my menu item detail", menu_header_id: mh.id, position: 1, is_enabled: true }}
      MenuItem.count.should eq(1)
      JSON.parse(response.body)['status'].should eq('success')
      JSON.parse(response.body)['preceding_item_id'].should eq(nil)
      response.code.should eq('200')
    end

Is this style ok? Or are there other / better ways to test a JSON api? Should these be feature specs?

Upvotes: 0

Views: 3379

Answers (1)

Matt Sanders
Matt Sanders

Reputation: 10865

Consider using request specs instead. They will allow you to run full stack tests including both the controller and the view layer without using render_views in your controller spec.

Here is a good article which gets into some of the details.

Upvotes: 1

Related Questions