require 'rails_helper' module Logistics module Core RSpec.describe EslTransportTariffRatesController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { esl_transport_tariff_id: create(:esl_transport_tariff).id, qt_from: 100, qt_to: 200, rate: 10 } } let(:invalid_attributes) { { esl_transport_tariff_id: nil, qt_from: 100, qt_to: 200, rate: 10 } } describe 'GET #index' do it 'gets all esl transport tariff rates' do 2.times { create(:esl_transport_tariff_rate) } get :index, format: :json result = JSON(response.body) expect(result['data'].count).to eq 2 end end describe "GET #filter_by_tariff" do it "gets filtered tariff rates" do esl_tariff = create(:esl_transport_tariff) esl_tariff_rate = create(:esl_transport_tariff_rate, :esl_transport_tariff => esl_tariff) 2.times { create(:esl_transport_tariff_rate) } get :filter_by_tariff, params: { :id => esl_tariff.id }, format: :json result = JSON(response.body) expect(result['data'].count).to eq 1 expect(result['data'][0]['qt_from']).to eq esl_tariff_rate.qt_from expect(result['data'][0]['qt_to']).to eq esl_tariff_rate.qt_to expect(result['data'][0]['rate']).to eq esl_tariff_rate.rate 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_rate => valid_attributes }, format: :json }.to change(EslTransportTariffRate, :count).by(1) end it 'should return a success message' do post :create, params: { :esl_transport_tariff_rate => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('ESL transport tariff rate saved successfully!') end end context 'with invalid params' do it 'should return an error message' do post :create, params: { :esl_transport_tariff_rate => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("ESL transport tariff rate Esl transport tariff can't be blank") end end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) { { rate: 40 } } it 'updates the requested esl transport tariff' do tariff_rate = create(:esl_transport_tariff_rate) old_data = tariff_rate.rate put :update, params: { :id => tariff_rate.to_param, :esl_transport_tariff_rate => new_attributes }, format: :json tariff_rate.reload expect(tariff_rate.rate).not_to eq old_data end it 'should return a success message' do tariff_rate = create(:esl_transport_tariff_rate) put :update, params: { :id => tariff_rate.to_param, :esl_transport_tariff_rate => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('ESL transport tariff rate updated successfully!') end end context 'with invalid params' do it 'should return an error message' do tariff_rate = create(:esl_transport_tariff_rate) put :update, params: { :id => tariff_rate.to_param, :esl_transport_tariff_rate => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("ESL transport tariff rate Esl transport tariff can't be blank") end end end end end end