Open1

rspec-rails

izszzzizszzz

respond_with (my custom method)

one liner expect http status code

  • support rspec-opeapi
  1. copy code
  2. make respond_with.rb file in support directory
  3. paste code
  4. called spec file need classname
RSpec.describe "User" or User, type request

Syntax

params type
HTTP_METHOD require/string
ACTION require/string
HTTP_STATUS_CODE require/string
let(:id) number
let(:params) parameter
  describe 'HTTP_METHOD /ACTION' do
    context '' do
      let(:id) { create(:user).id }
      let(:params) { { user: attributes_for(:user) } }

      it_behaves_like 'respond_with', HTTP_STATUS_CODE
    end
  end

Examples

Rspec.describe "Users", type: :request do
  describe 'GET /index' do
    context 'should success' do
      it_behaves_like 'respond_with', :success
    end
  end

  describe 'GET /show' do
    context 'should be success' do
      let(:id) { create(:user).id }

      it_behaves_like 'respond_with', :success
    end
  end

  describe 'POST /create' do
    context 'should be success' do
      let(:params) { { user: attributes_for(:user) } }

      it_behaves_like 'respond_with', :unprocessable_entity
    end
  end

  describe 'PATCH /update' do
    context 'should be success' do
      let(:id) { create(:user).id }
      let(:params) { { user: attributes_for(:user) } }

      it_behaves_like 'respond_with', :unprocessable_entity
    end
  end

  describe 'POST /create json' do
    context 'should be success' do
      let(:params) { { user: } }

      it_behaves_like 'respond_with', :created, openapi: true
    end
  end
end