require 'rails_helper' module Logistics module Core RSpec.describe PaymentTermsController, 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 payment terms' do 2.times { create(:payment_term) } 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 payment term' do expect { post :create, params: { :lookup => valid_attributes }, format: :json }.to change(PaymentTerm, :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('Payment term 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("Payment term 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 payment term' do lookup = create(:payment_term) 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(:payment_term) put :update, params: { :id => lookup.to_param, :lookup => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Payment term updated successfully!') end end context 'with invalid params' do it 'should return an error message' do lookup = create(:payment_term) put :update, params: { :id => lookup.to_param, :lookup => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Payment term Code can't be blank") end end end end end end