使用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