require 'rails_helper' module Logistics module Core RSpec.describe CustomsOfficesController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { code: FFaker::Name.name, name: FFaker::Name.name, location: FFaker::Name.name } } let(:invalid_attributes) { { code: nil, name: FFaker::Name.name, location: FFaker::Name.name } } describe 'GET #index' do it 'gets all customs offices' do 2.times { create(:customs_office) } 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 customs office' do expect { post :create, params: {customs_office: valid_attributes}, format: :json }.to change(CustomsOffice, :count).by(1) end it 'should return a success message' do post :create, params: {customs_office: valid_attributes}, format: :json result = JSON(response.body) expect(result['message']).to eq('Customs office saved successfully!') end end context 'with invalid params' do it 'should return an error message' do post :create, params: {customs_office: invalid_attributes}, format: :json result = JSON(response.body) expect(result['errors']).to include("Customs office 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 customs office' do customs_office = create(:customs_office) old_data = customs_office.code put :update, params: {id: customs_office.to_param, customs_office: new_attributes}, format: :json customs_office.reload expect(customs_office.code).not_to eq old_data end it 'should return a success message' do customs_office = create(:customs_office) put :update, params: {id: customs_office.to_param, customs_office: valid_attributes}, format: :json result = JSON(response.body) expect(result['message']).to eq('Customs office updated successfully!') end end context 'with invalid params' do it 'should return an error message' do customs_office = create(:customs_office) put :update, params: {id: customs_office.to_param, customs_office: invalid_attributes}, format: :json result = JSON(response.body) expect(result['errors']).to include("Customs office Code can't be blank") end end end end end end