【问题标题】:puts and binding.pry output is not visible in test environmentputs 和 binding.pry 输出在测试环境中不可见
【发布时间】:2017-05-17 00:58:54
【问题描述】:

我使用 minitest 和 rake 任务来运行测试。我使用标准记者(这是来自测试助手):

Minitest::Reporters.use!(
  Minitest::Reporters::DefaultReporter.new(fast_fail: true, slow_count: 3),
  # show single tests with the spec reporter
  # MiniTest::Reporters::SpecReporter.new,
  ENV, Minitest.backtrace_filter
)

这是我的 test.rake:

require 'rake/testtask'

Rake::Task['test:run'].clear
Rake::Task['test:decorators'].clear

namespace :test do
  task run: ['test:decorators', 'test:models', 'test:controllers', 'test:mailers', 'test:integration']

  Rake::TestTask.new(decorators: 'test:prepare') do |t|
    t.pattern = 'test/decorators/**/*_test.rb'
  end
end

我还在 Gemfile 中安装了 pry:

group :pry, :test do
  gem 'pry-rails'
  gem 'pry-byebug'
end

我无法在测试中使用它的问题。我可以用binding_pry 添加断点,但我看不到调用函数的输出。我也无法使用puts 记录任何内容(来自撬动和直接来自测试)。我在终端和日志文件中都没有看到这个输出。目前我唯一可以使用的是 Rails 记录器。但这并不令人满意。如何在终端中显示 pry 和 puts 的输出?

【问题讨论】:

    标签: ruby-on-rails testing stdout minitest puts


    【解决方案1】:

    因为您在测试环境中,所以应用了不同的日志记录设置,而不是在默认的 rails 控制台中。因此,它不会在您的 STDOUT 中查看日志,而是转到某个文件(或者您的测试环境已设置)。

    要解决此问题,在使用 pry 时,您可以在本地 ~/.pryrc file for customisation and configuration 上设置特定的 pry 设置。这样做的好处是不会“污染”您的真实测试环境,并且它仅在您的工作站本地。

    把它放在你的 ~/.pryrc 文件中:

    Pry.config.hooks.add_hook(:before_session, :rails) do
      if defined?(Rails)
        ActiveRecord::Base.logger = Logger.new(STDOUT)
      end
    end
    

    取自Active::Record Logger to STDOUT with pry

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多