检查RSpec中的响应是否为JSON

Are*_*rel 3 rspec ruby-on-rails

我觉得这很简单,但我找不到怎么做.

我想要做的就是编写一个如果返回json文件而不是HTML文件则传递的规范.

这就是我现在的测试:

    require 'spec_helper'

    describe "sessions" do
      before do
        @program =FactoryGirl.create(:program)
        @user = FactoryGirl.create(:user)
      end

      describe "user" do
        it "is logged in" do
        post "/api/v1/login", user_login: {email: @user.email, password: @user.password }
        response.status.should be(201)
        # response.format.should be(:json) # Can I do something like this?
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

Edg*_*ons 6

我建议你检查meagar已经注意到的实际内容.在我的项目中,我使用这个gem更容易匹配:https://github.com/collectiveidea/json_spec

它提供了方便的匹配器来轻松匹配json内容:be_json_eql include_json have_json_path have_json_type have_json_size

response.body.should include_json("some_field: 'it definitely has'")
Run Code Online (Sandbox Code Playgroud)