【问题标题】:How do I create an rspec test that validates a JSON response?如何创建验证 JSON 响应的 rspec 测试?
【发布时间】:2011-05-13 16:45:18
【问题描述】:

我有一个带有 def inbox. 方法的组控制器

如果用户是组成员,则收件箱返回 JSON 对象。

如果用户不是会员,则收件箱应根据 CanCan 权限重定向。

如何编写 rspec 来测试这两个用例?

当前规格:

require 'spec_helper'

describe GroupsController do
  include Devise::TestHelpers

  before (:each) do
    @user1 = Factory.create(:user)
    @user1.confirm!
    sign_in @user1
    @group = Factory(:group)
    @permission_user_1 = Factory.create(:permission, :user => @user1, :creator_id => @user1.id, :group => @group)
  end

  describe "GET inbox" do
    it "should be successful" do
      get inbox_group_path(@group.id), :format => :json
      response.should be_success
    end
  end
end

路线:

inbox_group GET /groups/:id/inbox(.:format) {:controller=>"groups", :action=>"inbox"}

路线文件:

resources :groups do
  member do
    get 'vcard', 'inbox'
  end
  ....
end

【问题讨论】:

    标签: json ruby-on-rails-3 testing rspec cancan


    【解决方案1】:

    我就是这样做的:

    describe "GET index" do
      it "returns correct JSON" do
        # @groups.should have(2).items
        get :index, :format => :json
        response.should be_success
        body = JSON.parse(response.body)
        body.should include('group')
        groups = body['group']
        groups.should have(2).items
        groups.all? {|group| group.key?('customers_count')}.should be_true
        groups.any? {|group| group.key?('customer_ids')}.should be_false
      end
    end
    

    我没有使用 cancan,因此我无法在这部分提供帮助。

    【讨论】:

    • 谢谢 试过了,但我得到一个错误:“失败/错误:get :inbox, :format => :json ActionController::RoutingError: No route matches {:controller=>"groups", : format=>:json, :action=>"inbox"} # ./controllers/groups_controller_spec.rb:19 " 考虑到 rake 路由产量,这很奇怪:inbox_group GET /groups/:id/inbox(.:format) {:controller =>"群组", :action=>"收件箱"}
    • 尝试提供使用url_for获取的路径-apidock.com/rails/ActionDispatch/Integration/RequestHelpers/get
    • 奇怪。对不起,不知道我做错了什么。 “失败:1)GroupsController GET索引应该成功失败/错误:获取inbox_group_path(@group.id),:格式=>:json ActionController::路由错误:没有路由匹配{:控制器=>“组”,:格式= >:json, :action=>"/groups/33/inbox"} # ./controllers/groups_controller_spec.rb:16"
    • 嗯.. 它现在看起来更像是 Rails 的错误 :)
    • 控制器方法的结尾是:render :json => {:convlist => @convList } 这可能是问题吗?
    【解决方案2】:

    有时验证response 是否包含有效的 JSON 并显示实际响应可能就足够了,下面是一个示例:

    it 'responds with JSON' do
      expect {
        JSON.parse(response.body)
      }.to_not raise_error, response.body
    end
    

    【讨论】:

      【解决方案3】:

      试试这个:

      _expected = {:order => order.details}.to_json
      response.body.should == _expected
      

      【讨论】:

        【解决方案4】:

        我认为您要做的第一件事是检查响应的类型是否正确,即它的 Content-Type 标头设置为 application/json,类似于:

        it 'returns JSON' do
          expect(response.content_type).to eq(Mime::JSON)
        end
        

        然后,根据您的情况,您可能需要检查响应是否可以解析为 JSON,例如 wik 建议:

        it 'responds with JSON' do
          expect {
            JSON.parse(response.body)
          }.to_not raise_error
        end
        

        如果您觉得用于检查 JSON 响应有效性的两个测试太多,您可以将上述两个合并为一个测试。

        【讨论】:

          【解决方案5】:

          要断言 JSON,您也可以这样做:

          ActiveSupport::JSON.decode(response.body).should == ActiveSupport::JSON.decode(
            {"error" => " An email address is required "}.to_json
          )
          

          This 博客提供了更多想法。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-16
            相关资源
            最近更新 更多