我使用minitest和rake task来运行测试。我使用标准的报告器(这是来自测试助手的):
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
记录任何东西(无论是从pry还是直接从测试)。我在终端和日志文件中都看不到这个输出。目前我唯一能用的就是Rails记录器。但这并不是让人眼花缭乱如何在终端中显示pry和puts的输出?
发布于 2018-05-28 10:17:57
因为您是在测试环境中,所以应用了不同的日志记录设置,而不是在默认的rails控制台中。因此,它不是在STDOUT中看到日志,而是转到某个文件中(或者无论您测试环境是如何设置的)。
要解决此问题,在使用撬动时,可以在本地~/.pryrc
file for customisation and configuration上设置特定的撬动设置。这样做的好处是不会“污染”你的真实测试环境,而且它只对你的工作站是本地的。
将以下内容放入~/.pryrc文件中:
Pry.config.hooks.add_hook(:before_session, :rails) do
if defined?(Rails)
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
end
https://stackoverflow.com/questions/44013838
复制