# RSpec ## Troubleshooting and Debugging * Git Bisect 執行步驟: 1. git bisect start 2. 在出錯的commit標記bad `git bisect bad` 3. 往回查找,直到commit內部不包含出錯的commit,並標記為good `git bisect good` 4. `git bisect start HEAD (good one)` 可以看到還有幾個與下一個需要查找的 5. checkout到下一個可能出問題的commit進行測試,並標記好壞 6. 反覆步驟四與五直到找到bug源頭 基本指令 ```ruby= git bisect start # 開始搜尋 git bisect start HEAD (commit) # 開始搜尋,並標記 HEAD 為壞的,commit 為好的。 git bisect reset # 停止搜尋 git bisect bad # 標記目前版本為壞的 git bisect bad (commit) # 標記 commit 為壞的 git bisect good # 標記目前版本為壞的 git bisect good (commit) # 標記 commit 為好的 git bisect skip # 跳過目前版本 git bisect reset (commit) # 重置 commit 標記 git bisect log # 查看操作記錄 git bisect replay <file-path> # 自動執行 git bisect log 所記錄的內容 git bisect visualize # 視覺化檢視 ``` ## Running Tests Faster and Running Faster Tests * RSpec 1. 測試多個(單個)rb檔 `rspec spec/models/project_spec.rb spec/models/task_spec.rb` 2. 單一測試 `rspec spec/models/task_spec.rb.rb:5` 3. 測試巢狀結構 `rspec spec/models/task_spec.rb.rb[1:1:1]` 4. 使用標示 ```ruby= it "should run when I ask for focused tests", :focus do end it "should not run when I ask for focused tests" do end ``` 測試有focus標籤的```rspec --tag focus``` 不測試有focus標籤的```rspec --tag ~focus``` * Minitest 1. 測試整個檔案 `rake test test/models/task_test.rb` 2. 單一測試 ``` ruby -Ilib:test test/models/task_test.rb -n test_a_completed_task_is_complete ``` 等同於 ``` ruby -Ilib:test test/models/task_test.rb -l 5 ``` * Running Rails in the Background 由於執行rspec時會載入環境,而spring是預加載套件,提升測試效率。 * Running Tests Automatically with [Guard](https://github.com/guard/guard) 更動程式碼後儲存,會自動執行測試