Sha256: 884fb70f7988163f4041f8303e2d03ad0528d6202d263cda4e42f49e7c8de3de

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

module LightServiceExt
  RSpec.describe WithErrorHandler do
    unless defined? Rails
      module Rails
      end
    end

    unless defined? Rails::ActiveRecordError
      module Rails
        class ActiveRecordError < StandardError
          def model; end
        end
      end
    end

    let(:implementor_class) do
      Class.new do
        extend WithErrorHandler
      end
    end

    let(:key) { :key }
    let(:key_error) { 'invalid' }
    let(:messages) { { key => [key_error] } }
    let(:error_message) { 'some-error' }
    let(:errors) { OpenStruct.new(messages: messages) }
    let(:model) { OpenStruct.new(errors: errors) }
    let(:results_ctx_proc) { -> { ApplicationContext.make_with_defaults } }
    let(:ctx) { ApplicationContext.make_with_defaults }
    let(:error) { Rails::ActiveRecordError.new(error_message) }

    before { allow(LightServiceExt.config.logger).to receive(:error) }

    describe '.with_error_handler' do
      subject(:errors_handled_ctx) do
        implementor_class.with_error_handler(ctx: ctx, &results_ctx_proc)
      end

      context 'with active record error' do
        let(:results_ctx_proc) { -> { raise error } }

        context 'with error including model' do
          before { allow(error).to receive(:model) { model } }

          it 'adds model errors' do
            expect(errors_handled_ctx.failure?).to be_truthy
            expect(errors_handled_ctx.errors).to eql({ key => key_error })
            expect(errors_handled_ctx.internal_only[:error_info]).to be_an_instance_of(ErrorInfo)
            expect(LightServiceExt.config.logger).to have_received(:error)
          end
        end

        it 'adds errors to context' do
          expect(errors_handled_ctx.failure?).to be_truthy
          expect(errors_handled_ctx.errors).to eql({ base: error_message })
          expect(errors_handled_ctx.internal_only[:error_info]).to be_an_instance_of(ErrorInfo)
          expect(LightServiceExt.config.logger).to have_received(:error)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
light-service-ext-0.1.2 spec/light-service-ext/with_error_handler_spec.rb