Type:
Module

Provides some helpers to deal with testing log subscribers by setting up notifications. Take for instance Active Record subscriber tests:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class SyncLogSubscriberTest < ActiveSupport::TestCase
  include ActiveSupport::LogSubscriber::TestHelper
 
  def setup
    ActiveRecord::LogSubscriber.attach_to(:active_record)
  end
 
  def test_basic_query_logging
    Developer.all.to_a
    wait
    assert_equal 1, @logger.logged(:debug).size
    assert_match(/Developer Load/, @logger.logged(:debug).last)
    assert_match(/SELECT \* FROM "developers"/, @logger.logged(:debug).last)
  end
end

All you need to do is to ensure that your log subscriber is added to Rails::Subscriber, as in the second line of the code above. The test helpers are responsible for setting up the queue, subscriptions and turning colors in logs off.

The messages are available in the @logger instance, which is a logger with limited powers (it actually does not send anything to your output), and you can collect them doing @logger.logged(level), where level is the level used in logging, like info, debug, warn and so on.

setup
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::LogSubscriber/ActiveSupport::LogSubscriber::TestHelper

setup() Instance Public methods

2025-01-10 15:47:30
teardown
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::LogSubscriber/ActiveSupport::LogSubscriber::TestHelper

teardown() Instance Public methods

2025-01-10 15:47:30
flush
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::LogSubscriber/ActiveSupport::LogSubscriber::TestHelper/ActiveSupport::LogSubscriber::TestHelper::MockLogger

flush() Instance Public methods

2025-01-10 15:47:30
set_logger
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::LogSubscriber/ActiveSupport::LogSubscriber::TestHelper

set_logger(logger) Instance Public methods Overwrite if you use another logger

2025-01-10 15:47:30
wait
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::LogSubscriber/ActiveSupport::LogSubscriber::TestHelper

wait() Instance Public methods Wait notifications to be published.

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::LogSubscriber/ActiveSupport::LogSubscriber::TestHelper/ActiveSupport::LogSubscriber::TestHelper::MockLogger

new(level = DEBUG) Class Public methods

2025-01-10 15:47:30
logged
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::LogSubscriber/ActiveSupport::LogSubscriber::TestHelper/ActiveSupport::LogSubscriber::TestHelper::MockLogger

logged(level) Instance Public methods

2025-01-10 15:47:30
method_missing
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::LogSubscriber/ActiveSupport::LogSubscriber::TestHelper/ActiveSupport::LogSubscriber::TestHelper::MockLogger

method_missing(level, message = nil) Instance Public methods

2025-01-10 15:47:30