Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for controller concern #13

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading