Rake 是 Ruby 版的 Make,以 Rakefile 定義任務相依關係與動作。Rails 專案產生時自動建立 Rakefile,內建大量實用任務。
| 指令 | 功能 |
|---|---|
rake | 執行所有測試 (單元+功能+整合) |
rake test:units | 僅執行模型單元測試 |
rake test:functionals | 僅執行控制器功能測試 |
rake test:integration | 僅執行整合測試 |
rake test:recent | 執行最近修改過的測試 |
rake clone_structure_to_test | 將開發資料庫結構複製到測試資料庫 |
# 執行特定測試檔
ruby test/functional/search_controller_test.rb
# 執行特定測試方法
ruby test/functional/search_controller_test.rb -n test_search
ruby test/functional/search_controller_test.rb -n /search/ # 正則匹配方法名
搭配 DamageControl、CruiseControl.rb 或現代 Jenkins/GitLab CI:
rakedepot> rake stats
輸出詳細統計表:
+----------------------+-------+-------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers | 251 | 213 | 5 | 29 | 5 | 5 |
| Helpers | 14 | 13 | 0 | 1 | 0 | 11 |
| Models | 113 | 101 | 6 | 16 | 2 | 4 |
| Libraries | 0 | 0 | 0 | 0 | 0 | 0 |
| Components | 0 | 0 | 0 | 0 | 0 | 0 |
| Integration tests | 0 | 0 | 0 | 0 | 0 | 0 |
| Functional tests | 241 | 192 | 8 | 30 | 3 | 4 |
| Unit tests | 88 | 74 | 4 | 11 | 2 | 4 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total | 707 | 593 | 23 | 87 | 3 | 4 |
+----------------------+-------+-------+---------+---------+-----+-------+
Code LOC: 327 Test LOC: 266 Code to Test Ratio: 1:0.8
指標說明:
使用 rcov gem(現代專案建議用 SimpleCov):
gem install rcov
rcov test/functional/search_controller_test.rb
產生 coverage/ 目錄下的 HTML 報告:
將覆蓋率門檻納入 CI 流程,確保核心邏輯完整測試。