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