Skip to content

Commit

Permalink
Add test coverage for controller concern
Browse files Browse the repository at this point in the history
  • Loading branch information
dickdavis committed Sep 21, 2023
1 parent bfbf1a8 commit e5309d7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/event_logger_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ def self.logger
def self.log(*tags, **params)
logger.log(*tags, **params)
end

def self.reset
@logger = nil
end
end
76 changes: 76 additions & 0 deletions spec/app/controllers/concerns/event_logger_rails/loggable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

require 'rails_helper'

class DummyController < ActionController::Base
include EventLoggerRails::Loggable

def test_one
log_event :info, 'event_logger_rails.event.testing'
head :no_content
end

def test_two
log_event :info, 'event_logger_rails.event.testing', test: 'two'
head :no_content
end
end

RSpec.describe EventLoggerRails::Loggable, type: :request do
let(:params) { { foo: 'bar' } }
let(:data_from_request) do
{
action: controller.action_name,
controller: controller.controller_name.camelcase,
method: request.method,
parameters: params.to_json,
path: request.path,
remote_ip: request.remote_ip
}
end

let(:logger_spy) { instance_spy(EventLoggerRails::EventLogger) }

before do
allow(EventLoggerRails::EventLogger).to receive(:new).and_return(logger_spy)

Rails.application.routes.draw do
get :test_one, to: 'dummy#test_one', as: :test_one
get :test_two, to: 'dummy#test_two', as: :test_two
end

EventLoggerRails.setup do |config|
config.logger_levels = %i[info]
end
end

context 'without additional data provided' do
before { EventLoggerRails.reset }

it 'calls the event logger' do
get(test_one_path, params:)
expect(logger_spy)
.to have_received(:log)
.with(
:info,
'event_logger_rails.event.testing',
data_from_request
)
end
end

context 'with additional data provided' do
before { EventLoggerRails.reset }

it 'calls the event logger' do
get(test_two_path, params:)
expect(logger_spy)
.to have_received(:log)
.with(
:info,
'event_logger_rails.event.testing',
data_from_request.merge({ test: 'two' })
)
end
end
end

0 comments on commit e5309d7

Please sign in to comment.