require 'rails_helper' module Logistics module Core RSpec.describe DeclarationTypesController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { code: FFaker::Name.name, name: FFaker::Name.name, transaction_type_id: create(:transaction_type).id } } let(:invalid_attributes) { { code: '', name: FFaker::Name.name, transaction_type_id: create(:transaction_type).id } } describe "GET #index" do it "assigns all declaration types as @declaration_types" do dt = create(:declaration_type) get :index, format: :json result = JSON(response.body) expect(result['data'][0]['code']).to eq dt.code expect(result['data'][0]['name']).to eq dt.name end end describe "GET #filter_by_transaction_type" do it "assigns filtered declaration types as @declaration_types" do tt = create(:transaction_type) dt = create(:declaration_type, :transaction_type => tt) create(:declaration_type) create(:declaration_type) get :filter_by_transaction_type, params: { :id => tt.id }, format: :json result = JSON(response.body) expect(result['data'].count).to eq 1 expect(result['data'][0]['code']).to eq dt.code expect(result['data'][0]['name']).to eq dt.name end end describe "POST #create" do context "with valid params" do it "creates a new declaration type" do expect { post :create, params: { :declaration_type => valid_attributes }, format: :json }.to change(DeclarationType, :count).by(1) end it "should return a success message" do post :create, params: { :declaration_type => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Declaration type saved successfully!') end end context "with invalid params" do it 'should return an error message' do post :create, params: { :declaration_type => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Declaration type 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 declaration type" do declaration_type = create(:declaration_type) old_data = declaration_type.name put :update, params: { :id => declaration_type.to_param, :declaration_type => new_attributes }, format: :json declaration_type.reload expect(declaration_type.name).not_to eq old_data end it "should return a success message" do declaration_type = create(:declaration_type) put :update, params: { :id => declaration_type.to_param, :declaration_type => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Declaration type updated successfully!') end end context "with invalid params" do it "should return an error message" do declaration_type = create(:declaration_type) put :update, params: { :id => declaration_type.to_param, :declaration_type => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to include("Declaration type Code can't be blank") end end end end end end