-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test coverage for controller concern
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
spec/app/controllers/concerns/event_logger_rails/loggable_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |