require 'rails_helper' module Logistics module Core RSpec.describe DeclarantsController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { first_name: FFaker::Name.name, last_name: FFaker::Name.name, number: FFaker::Name.name } } let(:invalid_attributes) { { first_name: '', last_name: FFaker::Name.name, number: FFaker::Name.name } } describe 'GET #index' do it 'gets all declarants' do 2.times { create(:declarant) } 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 declarant' do expect { post :create, params: { :declarant => valid_attributes }, format: :json }.to change(Declarant, :count).by(1) end it 'should return a success message' do post :create, params: { :declarant => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Declarant saved successfully!') end end context 'with invalid params' do it 'should return an error message' do post :create, params: { :declarant => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Declarant First name can't be blank") end end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) { { first_name: FFaker::Name.name } } it 'updates the requested declarant' do declarant = create(:declarant) old_data = declarant.first_name put :update, params: { :id => declarant.to_param, :declarant => new_attributes }, format: :json declarant.reload expect(declarant.first_name).not_to eq old_data end it 'should return a success message' do declarant = create(:declarant) put :update, params: { :id => declarant.to_param, :declarant => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Declarant updated successfully!') end end context 'with invalid params' do it 'should return an error message' do declarant = create(:declarant) put :update, params: { :id => declarant.to_param, :declarant => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Declarant First name can't be blank") end end end end end end