红宝石:无法与rspec一起使用记录器 [英] ruby: unable to use logger with rspec

查看:56
本文介绍了红宝石:无法与rspec一起使用记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日志记录添加到简单的rspec测试中.我正在使用Watir在规格范围内驱动Chrome,并且工作正常.我无法使用记录器"库获取日志.

I am trying to add logging to a simple rspec test. I am using Watir to drive Chrome within the spec and this works fine. I am unable to get logs using the "Logger" library.

这是我的规范:

require 'rubygems'
require 'watir-webdriver'
require 'rspec'
require 'logger'

describe 'ui tests' do
  let(:browser) { browser ||= Watir::Browser.new :chrome }

  let(:log) {
    log = Logger.new(STDOUT)
    log = Logger.new('watir.tests.log', 'daily')
    log.level = Logger::DEBUG
  }

  before {
    browser.goto 'http://translate.google.com'
  }
  after { browser.close }

  context 'simple tests' do
    it 'simple test' do
      log.info("Running simple test")
      browser.text_field(:id => "source").set("ost")

      # actual test/asserts here

    end
  end
end

问题是我无法在示例中调用任何日志记录方法,例如 log.info.我收到此错误:

The problem is that I am unable to call any logging method such as log.info inside the example. I get this error:

Failures:

  1) ui tests simple tests simple test
Failure/Error: log.info("Running simple test")
NoMethodError:
  undefined method `info' for 0:Fixnum

这为什么会失败?这是范围问题吗?如果我注释掉 log.info ,则说明运行正常.我可以在示例内对浏览器"对象使用任何方法(例如,对browser.text_field的调用).

Why is this failing? Is it a scope issue? If I comment out the log.info the spec runs fine. I can use any methods on the "browser" object (for example, the call to browser.text_field) inside the example without a problem.

我要去哪里错了?

推荐答案

log 的值是 let(:log){} 块中最后计算的值,这是 Logger :: DEBUG .该常量只对应 Fixnum 0,并且没有名为0的 info 方法.请尝试以下方法:

The value of log is the last thing evaluated inside the let(:log) { } block, which is Logger::DEBUG. This constant just corresponds to the Fixnum 0, and there's no method called info for 0. Try this instead:

let(:log) { 
  Logger.new(STDOUT).tap { |l| l.level = Logger::DEBUG }
}

顺便说一句,您要如何设置 log = Logger.new(STDOUT),然后立即重新分配 log = Logger.new('watir.tests.log',每日")?

By the way, what are you trying to do with setting log = Logger.new(STDOUT) and then immediately reassigning log = Logger.new('watir.tests.log', 'daily')?

这篇关于红宝石:无法与rspec一起使用记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆