require 'rails_helper' module Logistics module Core RSpec.describe CustomsTransportTariffsController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { route_id: create(:route).id, entry_point_id: create(:entry_point).id, total_distance: 100, distance_to_entry_point: 50, proportion: 50 } } let(:invalid_attributes) { { route_id: nil, entry_point_id: create(:entry_point).id, total_distance: 100, distance_to_entry_point: 50, proportion: 50 } } describe 'GET #index' do it 'gets all customs transport tariffs' do 2.times { create(:customs_transport_tariff) } 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 customs transport tariff' do expect { post :create, params: { :customs_transport_tariff => valid_attributes }, format: :json }.to change(CustomsTransportTariff, :count).by(1) end it 'should return a success message' do post :create, params: { :customs_transport_tariff => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Customs transport tariff saved successfully!') end end context 'with invalid params' do it 'should return an error message' do post :create, params: { :customs_transport_tariff => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Customs transport tariff Route can't be blank") end end end describe 'PUT #update' do context 'with valid params' do let(:new_attributes) { { total_distance: 200 } } it 'updates the requested customs transport tariff' do tariff = create(:customs_transport_tariff) old_data = tariff.total_distance put :update, params: { :id => tariff.to_param, :customs_transport_tariff => new_attributes }, format: :json tariff.reload expect(tariff.total_distance).not_to eq old_data end it 'should return a success message' do tariff = create(:customs_transport_tariff) put :update, params: { :id => tariff.to_param, :customs_transport_tariff => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Customs transport tariff updated successfully!') end end context 'with invalid params' do it 'should return an error message' do tariff = create(:customs_transport_tariff) put :update, params: { :id => tariff.to_param, :customs_transport_tariff => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Customs transport tariff Route can't be blank") end end end end end end