Class Index [+]

Quicksearch

ActiveSupport::LogSubscriber::TestHelper

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 is reponsible 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 do 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.

Public Instance Methods

set_logger(logger) click to toggle source

Overwrite if you use another logger in your log subscriber:

  def logger
    ActiveRecord::Base.logger = @logger
  end
    # File lib/active_support/log_subscriber/test_helper.rb, line 94
94:       def set_logger(logger)
95:         ActiveSupport::LogSubscriber.logger = logger
96:       end
setup() click to toggle source
    # File lib/active_support/log_subscriber/test_helper.rb, line 35
35:       def setup
36:         @logger   = MockLogger.new
37:         @notifier = ActiveSupport::Notifications::Fanout.new
38: 
39:         ActiveSupport::LogSubscriber.colorize_logging = false
40: 
41:         set_logger(@logger)
42:         ActiveSupport::Notifications.notifier = @notifier
43:       end
teardown() click to toggle source
    # File lib/active_support/log_subscriber/test_helper.rb, line 45
45:       def teardown
46:         set_logger(nil)
47:         ActiveSupport::Notifications.notifier = nil
48:       end
wait() click to toggle source

Wait notifications to be published.

    # File lib/active_support/log_subscriber/test_helper.rb, line 84
84:       def wait
85:         @notifier.wait
86:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.