require 'rails_helper' module Logistics module Core RSpec.describe DocumentTypesController, type: :controller do routes { Logistics::Core::Engine.routes } let(:valid_attributes) { { code: FFaker::Name.name, name: FFaker::Name.name, description: FFaker::Lorem.paragraph, document_type_category_id: create(:document_type_category).id, document_type_origin_id: create(:document_type_origin).id } } let(:invalid_attributes) { { code: nil, name: FFaker::Name.name, description: FFaker::Lorem.paragraph, document_type_category_id: create(:document_type_category).id, document_type_origin_id: create(:document_type_origin).id } } let(:valid_session) { {} } describe 'GET #index' do it 'assigns all document_types as @document_types' do document_type = create(:document_type) get :index, format: :json result = JSON(response.body) expect(result['data'][0]['code']).to eq document_type.code expect(result['data'][0]['name']).to eq document_type.name expect(result['data'][0]['document_type_category_id']).to eq document_type.document_type_category_id expect(result['data'][0]['document_type_origin_id']).to eq document_type.document_type_origin_id end end describe "GET #lookup" do it "gets document types as lookup" do dt = create(:document_type) h = { "id" => dt.id, "code" => dt.code, "name" => dt.name } get :lookup, format: :json result = JSON(response.body) expect(result['data'].count).to eq 1 expect(result['data'][0]).to eq h expect(result['data'][0].length).to eq 3 end end describe 'POST #create' do context 'with valid params' do it 'creates a new DocumentType' do expect { post :create, params: { :document_type => valid_attributes }, format: :json }.to change(DocumentType, :count).by(1) end it 'return success message' do post :create, params: { :document_type => valid_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq 'Document Type information saved successfully!' end end context 'with invalid params' do it 'return error message' do post :create, params: { :document_type => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to eq ["Document 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 document_type' do dt = create(:document_type) old_data = dt.name put :update, params: { :id => dt.to_param, :document_type => new_attributes }, format: :json dt.reload expect(dt.name).not_to eq old_data end it 'return success message' do dt = create(:document_type) put :update, params: { :id => dt.to_param, :document_type => new_attributes }, format: :json result = JSON(response.body) expect(result['message']).to eq('Document Type information updated successfully!') end end context 'with invalid params' do it 'returns an error message' do dt = create(:document_type) put :update, params: { :id => dt.to_param, :document_type => invalid_attributes }, format: :json result = JSON(response.body) expect(result['errors']).to eq ["Document Type Code can't be blank"] end end end end end end