我一直在使用 capybara 和 selenium-webdriver 编写 rspec 测试。几乎毫无疑问,每当我运行这些测试之一时,控制台输出就会消失。
例如:
~/code/code> bundle exec rspec spec/features/interactions_spec.rb
InteractionsSpec
~/code/code>
这就是我所看到的一切。浏览器启动,执行我编码的操作,但我没有看到通常的输出。
有时我会将此视为输出(耶!):
InteractionsSpec
login as admin works
login as user works
Finished in 16.84 seconds (files took 7.9 seconds to load)
2 examples, 0 failures
什么可能导致测试的输出消失?这使得编写测试几乎不可能 - 因为我不知道什么已经运行,什么已经通过,什么失败了,或者为什么他们失败了。
我正在使用这些 Gem,但执行 bundle 更新不会改变行为。
- capybara (2.4.4)
- capybara 截图(1.0.3)
- rspec (3.0.0)
- rspec-activemodel-mocks (1.0.1)
- rspec-collection_matchers (1.0.0)
- rspec-core (3.0.3)
- rspec 期望 (3.0.3)
- rspec-mocks (3.0.3)
- rspec-rails (3.0.2)
- rspec 支持 (3.0.3)
- selenium-webdriver (2.44.0)
- 应该匹配器 (2.6.2)
其他涉及的软件:
- Firefox 33.1.1(Chrome 39.0.2171.65(64 位)具有相同的行为)
- OSX 10.9.5
- Rails 4.1.4
更新 这似乎暂时解决了问题,即使 sleep 时间为 1 毫秒。然而,这只是一个临时修复,这个问题仍然存在。
RSpec.configure do |config|
config.before(:each, :type => :feature) do
sleep(0.5)
end
end
最佳答案
在我看来,记录器通过其他线程对 logger.silence() 的调用而被静音。
当您将 sleep() 放入代码中时,您就会屈服于修复日志记录级别的其他线程 - 这就是为什么 sleep 多长时间并不重要,而是上下文切换解决了问题.
当您升级 activesupport gem 时,此处提交 https://github.com/rails/activerecord-session_store/commit/f92d1135fc620cb4d65239ef286b267945bbbbc6解决问题(正如你所说)。阅读该提交,请注意 logger.silence 的新线程安全实现。这就是它解决您的问题的原因。
这里的 active_support/logger_silence.rb 的旧实现不是线程安全的:
require 'active_support/concern'
module LoggerSilence
extend ActiveSupport::Concern
included do
cattr_accessor :silencer
self.silencer = true
end
# Silences the logger for the duration of the block.
def silence(temporary_level = Logger::ERROR)
if silencer
begin
old_logger_level, self.level = level, temporary_level
yield self
ensure
self.level = old_logger_level
end
else
yield self
end
end
end
关于ruby-on-rails - caypbara/rspec/selenium-webdriver 无终端输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27113856/