【发布时间】:2017-12-02 06:48:56
【问题描述】:
我有以下模型测试:
describe 'Checking for errors' do
it 'should check for any jobs that have errors and log them' do
logger = Logger.new('log/go_job_errors_test.log')
code_location = create(:code_location_with_best_code_set)
code_location.jobs << create(:complete_job, status: 3, exception: 'I failed to complete')
CodeLocationJobFeeder.create(code_location_id: code_location.id, url: code_location.repository.url, status: 2)
CodeLocationJobFeeder.check_for_errors(1)
logger.expects(:info).with("I failed to complete")
end
使用以下型号代码:
def check_for_errors(amount)
processed.limit(amount).each do |code_location_job_feeder|
code_location = CodeLocation.includes(:jobs).find(code_location_job_feeder.code_location_id)
log_error(code_location) unless code_location.jobs.last.status != 3
end
end
private
def log_error(code_location)
logger = Logger.new('log/go_job_errors.log')
failed_job = code_location.jobs.last
logger.info do
"Job #{failed_job.id} with code location: #{code_location.id} failed. Exception: #{failed_job.exception}"
end
end
测试的要点是,当我执行CodeLocationJobFeeder.check_for_errors 时,我将有一个单独的日志文件,它会告诉我我想要的关于某些错误的所有信息。这在开发中运行良好,我得到了我想要的文件。但是,当我运行测试时,我在 log/go_job_errors_test.log 和 log/go_job_errors.log 中都得到了输出,输出如下:
log/go_job_errors.log
[2017-06-28T18:23:46.164141 #29604] INFO -- : Job 40 with code location: 93 failed. Exception: I failed to complete
这是我想要的内容,但在错误的文件中。这是第二个文件:
log/go_job_error_test.log
# Logfile created on 2017-06-28 18:40:59 +0000 by logger.rb/47272
内容错误的正确文件。
我一直在尝试应用这个 s.o.回答Logger test,但它似乎不太好用。有人可以帮忙指出我做错了什么吗?
【问题讨论】:
-
因为您在测试中创建的记录器会被忽略。没有人使用它。模型写入自己的记录器。
标签: ruby-on-rails ruby logging