require 'rails_helper' module Logistics module Core RSpec.describe ChargeableServicesController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { code: FFaker::Name.name, name: FFaker::Name.name, order: 1, chargeable_service_type_id: create(:chargeable_service_type).id } } let(:invalid_attributes) { { code: nil, name: FFaker::Name.name, order: 1, chargeable_service_type_id: create(:chargeable_service_type).id } } describe 'GET #index' do it 'gets all chargeable services' do 2.times { create(:chargeable_service) } get :index, format: :json result = JSON(response.body) expect(result['data'].count).to eq 2 end end describe 'POST #create' do context 'with valid params' do it 'creates a new chargeable service' do expect { post :create, params: {chargeable_service: valid_attributes}, format: :json }.to change(ChargeableService, :count).by(1) end it 'should return a success message' do post :create, params: {chargeable_service: valid_attributes}, format: :json result = JSON(response.body) expect(result['message']).to eq('Chargeable service saved successfully!') end end context 'with invalid params' do it 'should return an error message' do post :create, params: {chargeable_service: invalid_attributes}, format: :json result = JSON(response.body) expect(result['errors']).to include("Chargeable service Code can't be blank") end end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) { { code: FFaker::Name.name } } it 'updates the requested chargeable service' do chargeable_service = create(:chargeable_service) old_data = chargeable_service.code put :update, params: {id: chargeable_service.to_param, chargeable_service: new_attributes}, format: :json chargeable_service.reload expect(chargeable_service.code).not_to eq old_data end it 'should return a success message' do chargeable_service = create(:chargeable_service) put :update, params: {id: chargeable_service.to_param, chargeable_service: valid_attributes}, format: :json result = JSON(response.body) expect(result['message']).to eq('Chargeable service updated successfully!') end end context 'with invalid params' do it 'should return an error message' do chargeable_service = create(:chargeable_service) put :update, params: {id: chargeable_service.to_param, chargeable_service: invalid_attributes}, format: :json result = JSON(response.body) expect(result['errors']).to include("Chargeable service Code can't be blank") end end end end end end