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

  class SyncLogSubscriberTest < ActiveSupport::TestCase
    include ActiveSupport::LogSubscriber::TestHelper

    def setup
      ActiveRecord::LogSubscriber.attach_to(:active_record)
    end

    def test_basic_query_logging
      Developer.all
      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.

Methods
S
T
W
Classes and Modules
Instance Public methods
set_logger(logger)

Overwrite if you use another logger in your log subscriber:

  def logger
    ActiveRecord::Base.logger = @logger
  end
    # File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 97
97:       def set_logger(logger)
98:         ActiveSupport::LogSubscriber.logger = logger
99:       end
setup()
    # File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 36
36:       def setup
37:         @logger   = MockLogger.new
38:         @notifier = ActiveSupport::Notifications::Fanout.new
39: 
40:         ActiveSupport::LogSubscriber.colorize_logging = false
41: 
42:         @old_notifier = ActiveSupport::Notifications.notifier
43:         set_logger(@logger)
44:         ActiveSupport::Notifications.notifier = @notifier
45:       end
teardown()
    # File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 47
47:       def teardown
48:         set_logger(nil)
49:         ActiveSupport::Notifications.notifier = @old_notifier
50:       end
wait()

Wait notifications to be published.

    # File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 87
87:       def wait
88:         @notifier.wait
89:       end