0

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?

2
  • sure, why not. just check it out if it works for your project setup. Commented Jan 30, 2014 at 5:16
  • thx @phoet yeah, it does work - not really sure if it's a best practice (or whether that even matters). I'm a bit peaved by the online resources when dealing with testing and they're a bit non-commital about how to do it. Commented Jan 30, 2014 at 5:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.