[Rspec] FactoryGirl create vs build vs build_stubbed


減少不必要的 DB 操作來提高測試效率


  1. 每支測試檔案,只做一次 clear DB 確保測試檔案之間不互相影響
  2. 可以使用一個數據進行測試,就不要每個 example 重複創建,減少 let and let! 大量使用實例變量
  3. 情境判斷,盡可能使用 mock 數據方式,達到你要的測試結果
  4. 使用 FactoryGirl.build orFactoryGirl.build_stubbed

前 3 點,已經熟能生巧
第 4 點,是我一直抱著 try try 看去實做

[Rspec] Efficiently Speed Rspec test


author has deleted

前面 1 ~ 4 點,學習一些技巧和寫法,但是效能提升没特別提到
但文章的第 5 點 Efficiently load data records 得到几个方向

  1. Use let, rather than let!
  2. One is to use before_all instead of before_each in a test file
  3. Another is to have a global variables file, which loads an instance of each record before all tests start
  4. Another approach is to user FactoryBot’s build_stubbed method
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now