# encoding: utf-8 require 'spec_helper' describe ErrorHandler do let(:exception) { StandardError.new } context '.create' do it 'requires an exception class, a message and a exit code' do expect { ErrorHandler.create( exception: StandardError, message: 'This is a standard error', exit_code: 1, ) }.not_to raise_error end end context '.find' do it 'finds a suitable error handler' do ErrorHandler.create( exception: StandardError, message: 'This is a standard error', exit_code: 1, ) expect(ErrorHandler.find(exception)).not_to be_nil end it 'returns a default exception if the requested one cannote be found' do expect(ErrorHandler.find(RuntimeError.new).exception).to be(StandardError) end end context '#exception' do it 'returns the exception' do handler = ErrorHandler.create( exception: StandardError, message: 'This is a standard error', exit_code: 1, ) expect(handler.exception).to eq(StandardError) end end context '#run' do it 'runs the error handler' do handler = ErrorHandler.new( exception: StandardError, message: 'This is a standard error', exit_code: 1, ) handler.original_message = 'blub' LocalPac.ui_logger.level = :debug result = capture(:stderr) do begin handler.run rescue SystemExit => e expect(e.status).to eq(1) end end expect(result).to include('standard error') expect(result).to include('blub') end end end