require 'rails_helper' module Logistics module Core RSpec.describe AgencyCategoriesController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { code: 'GOA', name: 'Government Agency', description: 'Owned by the government' } } let(:invalid_attributes) { { code: nil, name: 'Government Agency', description: 'Owned by the government' } } let(:valid_session) { {} } describe 'GET #index' do it 'assigns all agency categories as @agency_categories' do agency = AgencyCategory.create! valid_attributes get :index, format: :json expect(JSON(response.body)['data'].to_json).to eq([agency].to_json) end end describe 'POST #create' do context 'with valid params' do it 'creates a new agency_categories' do expect { post :create, params: { :agency_category => valid_attributes }, format: :json }.to change(AgencyCategory, :count).by(1) end it 'should return a success message' do post :create, params: { :agency_category => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Agency Category information saved successfully!') end end context 'with invalid params' do it 'should return an error message' do post :create, params: { :agency_category => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Agency Category Code can't be blank") end end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) { { name: 'Government Body' } } it 'updates the requested agency_category' do agency_category = AgencyCategory.create! valid_attributes put :update, params: { :id => agency_category.to_param, :agency_category => new_attributes }, format: :json agency_category.reload expect(agency_category.name).not_to eq valid_attributes[:name] end it 'should return a success message' do agency_category = AgencyCategory.create! valid_attributes put :update, params: { :id => agency_category.to_param, :agency_category => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Agency Category information updated successfully!') end end context 'with invalid params' do it 'should return an error message' do agency_category = AgencyCategory.create! valid_attributes put :update, params: { :id => agency_category.to_param, :agency_category => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Agency Category Code can't be blank") end end end end end end