简体   繁体   中英

Check if response is JSON in RSpec

I thought this would be simple, but I can't find how to do it.

All I want to do is write a spec that passes if a json file is returned instead of an HTML file.

This is what my test looks like right now:

    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

I suggest You to check the actual content as meagar already noted. In my projects I have used this gem for easier matching: https://github.com/collectiveidea/json_spec

It provides handy matchers to easy match json content: be_json_eql include_json have_json_path have_json_type have_json_size

response.body.should include_json("some_field: 'it definitely has'")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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