2014-02-13 7 views
1

Я хочу проверить свой тип ответа как JSON в RSpec, но respond_with_content_type больше не доступен в shoulda-matchers.Как проверить тип JSON в RSpec?

Ссылка: https://github.com/thoughtbot/shoulda-matchers/issues/252

Я использую:

  • рельсы (4.0.2)
  • RSpec рельсы (2.14.1)
  • Shoulda-matchers (2.5.0)

Это мой контроллер:

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 

И это моя спецификация:

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 

Любые намеки будут большими для меня. Благодаря!

ответ

3

попробовать это

expect(response.headers["Content-Type"]).to eql("application/json; charset=utf-8") 
+0

круто! оно работает! Спасибо чувак! : D – bprayudha

Смежные вопросы