简体   繁体   English

使用rspec控制器测试JSON API

[英]using rspec controllers to test JSON API

I am writing some tests for a JSON API. 我正在为JSON API编写一些测试。 Would using controller specs with render_views be an acceptable way of doing this? 将控制器规范与render_views一起使用是否可以做到这一点? 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? 还是有其他/更好的方法来测试JSON API? Should these be feature specs? 这些应该是功能规格吗?

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. 它们将允许您运行完整的堆栈测试,包括控制器和视图层,而无需在控制器规格中使用render_views

Here is a good article which gets into some of the details. 这是一篇很好的文章 ,其中涉及了一些细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM