require 'rails_helper' module Mks module Rate RSpec.describe RateChangeRequestsController, type: :controller do routes { Mks::Rate::Engine.routes } before(:each) do u = create(:user) token = Mks::Auth::TokenAuth.issue(name: u.full_name, email: u.email, id: u.id) request.headers['Authorization'] = "Bearer #{token}" end let(:valid_attributes) do { reference_number: FFaker::Name.name, authorized_by: FFaker::Name.name, effective_on: Date.today, authorized_on: Date.today, rates: [ { chargeable_service_id: create(:chargeable_service).id, chargeable_service_name: create(:chargeable_service).name, equipment_item_id: create(:equipment_item).id, equipment_item_name: create(:equipment_item).name, base_rate: 10, utilization_rate: 20 } ], status: RateChangeRequest::SUBMITTED, created_by_id: create(:user).id, approved_by_id: create(:user).id } end let(:invalid_attributes) do { reference_number: nil, authorized_by: FFaker::Name.name, effective_on: Date.today, authorized_on: Date.today, rates: [ { chargeable_service_id: create(:chargeable_service).id, chargeable_service_name: create(:chargeable_service).name, equipment_item_id: create(:equipment_item).id, equipment_item_name: create(:equipment_item).name, base_rate: 10, utilization_rate: 20 } ], status: RateChangeRequest::SUBMITTED, created_by_id: create(:user).id, approved_by_id: create(:user).id } end describe 'GET #index' do it 'returns a success response' do create(:rate_change_request) get :index expect(response).to be_successful end end describe 'GET #show' do it 'returns a success response' do rate_change_request = create(:rate_change_request) get :show, params: { id: rate_change_request.to_param } expect(response).to be_successful end end describe 'POST #create' do context 'with valid params' do it 'creates a new RateChangeRequest' do expect do post :create, params: { rate_change_request: valid_attributes } end.to change(RateChangeRequest, :count).by(1) end it 'renders a JSON response with the new rate_change_request' do post :create, params: { rate_change_request: valid_attributes } expect(response).to have_http_status(:created) expect(response.content_type).to eq('application/json') end end context 'with invalid params' do it 'renders a JSON response with errors for the new rate_change_request' do post :create, params: { rate_change_request: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) do { authorized_by: FFaker::Name.name } end it 'updates the requested rate_change_request' do rate_change_request = create(:rate_change_request) put :update, params: { id: rate_change_request.to_param, rate_change_request: new_attributes } rate_change_request.reload expect(rate_change_request.authorized_by).to eq new_attributes[:authorized_by] end it 'renders a JSON response with the rate_change_request' do rate_change_request = create(:rate_change_request) put :update, params: { id: rate_change_request.to_param, rate_change_request: valid_attributes } expect(response).to have_http_status(:ok) expect(response.content_type).to eq('application/json') end end context 'with invalid params' do it 'renders a JSON response with errors for the rate_change_request' do rate_change_request = create(:rate_change_request) put :update, params: { id: rate_change_request.to_param, rate_change_request: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end end end end end end