Skip to content

Commit

Permalink
tests: Avoid stubbing of logger when testing the logger, improve logger
Browse files Browse the repository at this point in the history
Mixing the test debug failure logger and this new logger is causing unreliable tests when using the new test logger.
This implementation is complete with support for blocks and the other logger is now disabled for these tests.
  • Loading branch information
mattheworiordan committed Apr 30, 2018
1 parent 82ed37e commit 5a060c3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion spec/acceptance/realtime/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ def disconnect_transport(connection)
end
end

context 'deprecated #authorise' do
context 'deprecated #authorise', :prevent_log_stubbing do
let(:client_options) { default_options.merge(key: api_key, logger: custom_logger_object, use_token_auth: true) }
let(:custom_logger_object) { TestLogger.new }

Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/rest/auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ def coerce_if_time_value(field_name, value, params = {})
end
end

context 'deprecated #authorise' do
context 'deprecated #authorise', :prevent_log_stubbing do
let(:client_options) { default_options.merge(key: api_key, logger: custom_logger_object, use_token_auth: true) }
let(:custom_logger_object) { TestLogger.new }

Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/rest/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ def encode64(text)

context 'request_id generation' do
context 'Timeout error' do
context 'with option add_request_ids: true', :webmock do
context 'with option add_request_ids: true', :webmock, :prevent_log_stubbing do
let(:custom_logger_object) { TestLogger.new }
let(:client_options) { default_options.merge(key: api_key, logger: custom_logger_object, add_request_ids: true) }

Expand Down Expand Up @@ -1138,7 +1138,7 @@ def encode64(text)
end
end

context 'failed request logging' do
context 'failed request logging', :prevent_log_stubbing do
let(:custom_logger) { TestLogger.new }
let(:client_options) { default_options.merge(key: api_key, logger: custom_logger) }

Expand Down
13 changes: 10 additions & 3 deletions spec/support/test_logger_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# Class with standard Ruby Logger interface
# but it keeps a record of the lof entries for later inspection
#
# Recommendation: Use :prevent_log_stubbing attibute on tests that use this logger
#
class TestLogger
def initialize
@messages = []
end

SEVERITIES = [:fatal, :error, :warn, :info, :debug]
SEVERITIES.each do |severity|
define_method severity do |message|
@messages << [severity, message]
SEVERITIES.each do |severity_sym|
define_method(severity_sym) do |*args, &block|
if block
@messages << [severity_sym, block.call]
else
@messages << [severity_sym, args.join(', ')]
end
end
end

Expand Down

0 comments on commit 5a060c3

Please sign in to comment.