1

I want to validate my response type as a JSON in RSpec, but respond_with_content_type is no longer available in shoulda-matchers.

Reference: https://github.com/thoughtbot/shoulda-matchers/issues/252

I'm using:

  • rails (4.0.2)
  • rspec-rails (2.14.1)
  • shoulda-matchers (2.5.0)

This is my controller:

class BrandsController < ApplicationController
  def create
    @brand = Brand.new(permitted_params)
    if @brand.save
      render :json => permitted_params, :status => 200
    else
      render :nothing => true, :status => 500
    end
  end

  private 
  def permitted_params
    params.require(:brand).permit(:name)
  end
end

And this is my spec:

require 'spec_helper'

describe BrandsController do
  describe 'POST create' do
    context 'with empty data' do
      before(:each) { post :create, brand: { name: '' } }
      it { should respond_with 500 }
    end
    context 'with invalid data' do
      before(:each) { post :create, brand: { name: '123' } }
      it { should respond_with 500 }
    end
    context 'with valid data' do
      before(:each) { post :create, brand: { name: 'somebrand' } }
      it { should respond_with 200 }
      # should respond with JSON type
    end
  end
end

Any hints will be great for me. Thanks!

1 Answer 1

3

try this

expect(response.headers["Content-Type"]).to eql("application/json; charset=utf-8")
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.