提供一些帮助器,用于通过设置通知来处理日志订阅者测试。例如,Active Record 订阅者测试
class SyncLogSubscriberTest < ActiveSupport::TestCase
include ActiveSupport::LogSubscriber::TestHelper
setup do
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
您需要做的就是确保您的日志订阅者已添加到 Rails::Subscriber,如上面代码的第二行所示。测试帮助器负责设置队列和订阅,并关闭日志中的颜色。
消息可在 @logger 实例中获得,这是一个功能受限的记录器(实际上它不会向您的输出发送任何内容),您可以通过执行 @logger.logged(level) 来收集它们,其中 level 是记录中使用的级别,例如 info、debug、warn 等。
命名空间
方法
- S
- W
实例公共方法
set_logger(logger) 链接
如果您在日志订阅者中使用其他记录器,请覆盖。
def logger
ActiveRecord::Base.logger = @logger
end
源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 101 def set_logger(logger) ActiveSupport::LogSubscriber.logger = logger end
wait() 链接
等待发布通知。
源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/log_subscriber/test_helper.rb, line 92 def wait @notifier.wait end