使用Rails仅有5三年连续,我不认为自己是一个先进的Railser,但尽管如此,我将愉快地分享我的知识。 :-)
与任何打交道时,(除了一些非常,非常琐碎的)最主要的是写一个测试此错误。
有几次它发生了,我解决了这个错误在此阶段 - 例如,当错误是有关自动重新加载类,这是在积极发展,并在测试模式下关闭。
然后,我通常只是放置一些logger.debug
陈述,其中有很多inspect
和caller(0).join("\n\t")
。然后我非常仔细地检查日志文件。
因为“test.log中”能有几百兆,我永远记得到零之前,我跑我的测试。此外,我通常只运行一种测试方法,因为输出结果在其他情况下不可读。
我不使用专用的调试器。在一些老版本的ruby中,调试器停止工作,我学会了没有它的生活,并且从不回头。
一些实用程序,其可能是有用的:
在我~/.bashrc
定义的功能,它可以让我来调用一个单一的测试方法(或一组的方法):
$ testuj test/unit/user_test.rb -n test_name_validations
$ testuj test/unit/user_test.rb -n /_name_/
function testuj() {
if [ -n "${BUNDLE_GEMFILE}" ]
then
# This is a Rails3 project - it is run by `bundle exec`
ruby -I"lib:test" "[email protected]"
else
# This is a Rails1 project. No bundler.
ruby -e 'ARGV.each { |f| load f unless f =~ /^-/ ; break if f == "-n" }' "[email protected]"
fi
}
而这种方法可以帮助我记录和检查一些步骤的时间:
Object.module_eval do
def czekpoint(note = nil)
n = Time.now
$czekpoint_previous ||= n
$czekpoint_number ||= 0
$czekpoint_number += 1
t = n - $czekpoint_previous
msg = "CZEKPOINT: %2d %8.6f %s %s" % [$czekpoint_number, t, caller.first.to_s.in_yellow, note.to_s.in_red]
Rails.logger.debug msg # In older Rails it was RAILS_DEFAULT_LOGGER
STDERR.puts msg
$czekpoint_previous = n
end
end
+1因为这有效。裴是你的朋友。 – Trip