Sha256: 09727de20106e3a2ad631ac223286d30137ba60be4062e074ede203503970f9b

Contents?: true

Size: 1.82 KB

Versions: 13

Compression:

Stored size: 1.82 KB

Contents

require 'spec_helper'

describe Services::Base::CallLogger do
  it 'logs start with args and end with duration' do
    service = EmptyService.new
    logs = []
    allow(service).to receive(:log) do |message, *|
      logs << message
    end
    service.call 'foo', 'bar'
    expect(logs.first).to eq('START with args ["foo", "bar"]')
    expect(logs.last).to eq('END after 0.0 seconds')
  end

  it 'logs the caller' do
    service_calling_service, called_service = ServiceCallingService.new, EmptyService.new
    logs = []
    allow(called_service).to receive(:log) do |message, *|
      logs << message
    end

    # When Rails is not defined, the complete caller path should be logged
    service_calling_service.call called_service
    expect(logs).to include(/\ACALLED BY #{Regexp.escape PROJECT_ROOT.join(TEST_SERVICES_PATH).to_s}:\d+/)

    # When Rails is defined, only the caller path relative to Rails.root is logged
    class Rails
      def self.root; PROJECT_ROOT; end
    end
    logs = []
    service_calling_service.call called_service
    expect(logs).to include(/\ACALLED BY #{Regexp.escape TEST_SERVICES_PATH.to_s}:\d+/)
    Object.send :remove_const, :Rails

    # Caller paths from services lib folder should be filtered
    require 'services/call_proxy'
    logs = []
    Services::CallProxy.call(called_service, :call)
    expect(logs).to include(/\ACALLED BY #{Regexp.escape __FILE__}:\d+/)
  end

  if RUBY_VERSION > '2.1'
    it 'logs exceptions and exception causes' do
      service = NestedExceptionService.new
      logs = []
      allow(service).to receive(:log) do |message, *|
        logs << message
      end
      expect { service.call }.to raise_error(service.class::Error)
      %w(NestedError1 NestedError2).each do |error|
        expect(logs).to include(/\Acaused by: #{service.class}::#{error}/)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
services-1.3.0 spec/services/modules/call_logger_spec.rb
services-1.2.0 spec/services/modules/call_logger_spec.rb
services-1.1.1 spec/services/modules/call_logger_spec.rb
services-1.1.0 spec/services/modules/call_logger_spec.rb
services-1.0.0 spec/services/modules/call_logger_spec.rb
services-0.4.0 spec/services/modules/call_logger_spec.rb
services-0.3.4 spec/services/modules/call_logger_spec.rb
services-0.2.14 spec/services/modules/call_logger_spec.rb
services-0.2.13 spec/services/modules/call_logger_spec.rb
services-0.2.12 spec/services/modules/call_logger_spec.rb
services-0.2.11 spec/services/modules/call_logger_spec.rb
services-0.2.10 spec/services/modules/call_logger_spec.rb
services-0.2.9 spec/services/modules/call_logger_spec.rb