require 'rails_helper' module Logistics module Core RSpec.describe EslTransportTariffsController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { route_id: create(:route).id, storage_point: FFaker::Name.name } } let(:invalid_attributes) { { route_id: nil, storage_point: FFaker::Name.name } } describe 'GET #index' do it 'gets all esl transport tariffs' do 2.times { create(:esl_transport_tariff) } get :index, format: :json result = JSON(response.body) expect(result['data'].count).to eq 2 end it 'returns all serialized fields' do tariff = create(:esl_transport_tariff) get :index, format: :json result = JSON(response.body) expect(result['data'][0]['id']).to eq tariff.id expect(result['data'][0]['name']).to eq tariff.name expect(result['data'][0]['route_id']).to eq tariff.route_id expect(result['data'][0]['storage_point']).to eq tariff.storage_point end end describe 'POST #create' do context 'with valid params' do it 'creates a new esl transport tariff' do expect { post :create, params: { :esl_transport_tariff => valid_attributes }, format: :json }.to change(EslTransportTariff, :count).by(1) end it 'should return a success message' do post :create, params: { :esl_transport_tariff => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('ESL transport tariff saved successfully!') end end context 'with invalid params' do it 'should return an error message' do post :create, params: { :esl_transport_tariff => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("ESL transport tariff Route can't be blank") end end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) { { storage_point: FFaker::Name.name } } it 'updates the requested esl transport tariff' do tariff = create(:esl_transport_tariff) old_data = tariff.storage_point put :update, params: { :id => tariff.to_param, :esl_transport_tariff => new_attributes }, format: :json tariff.reload expect(tariff.storage_point).not_to eq old_data end it 'should return a success message' do tariff = create(:esl_transport_tariff) put :update, params: { :id => tariff.to_param, :esl_transport_tariff => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('ESL transport tariff updated successfully!') end end context 'with invalid params' do it 'should return an error message' do tariff = create(:esl_transport_tariff) put :update, params: { :id => tariff.to_param, :esl_transport_tariff => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("ESL transport tariff Route can't be blank") end end end end end end