Module: ActiveSupport::LogSubscriber::TestHelper
- Defined in:
- activesupport/lib/active_support/log_subscriber/test_helper.rb
Overview
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.
Instance Method Summary (collapse)
-
- (Object) set_logger(logger)
Overwrite if you use another logger in your log subscriber:.
- - (Object) setup
- - (Object) teardown
-
- (Object) wait
Wait notifications to be published.
Instance Method Details
- (Object) set_logger(logger)
Overwrite if you use another logger in your log subscriber:
def logger
ActiveRecord::Base.logger = @logger
end
94 95 96 |
# File 'activesupport/lib/active_support/log_subscriber/test_helper.rb', line 94 def set_logger(logger) ActiveSupport::LogSubscriber.logger = logger end |
- (Object) setup
35 36 37 38 39 40 41 42 43 |
# File 'activesupport/lib/active_support/log_subscriber/test_helper.rb', line 35 def setup @logger = MockLogger.new @notifier = ActiveSupport::Notifications::Fanout.new ActiveSupport::LogSubscriber.colorize_logging = false set_logger(@logger) ActiveSupport::Notifications.notifier = @notifier end |
- (Object) teardown
45 46 47 48 |
# File 'activesupport/lib/active_support/log_subscriber/test_helper.rb', line 45 def teardown set_logger(nil) ActiveSupport::Notifications.notifier = nil end |
- (Object) wait
Wait notifications to be published.
84 85 86 |
# File 'activesupport/lib/active_support/log_subscriber/test_helper.rb', line 84 def wait @notifier.wait end |