提供了一些用于处理日志订阅者测试的助手,通过设置通知来实现。例如 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