Thursday, May 22, 2014

How to write better rspec tests

It is a great issue of writing rspec tests because it needs a lot of practice to get used to it for a new programmer. I have found a guide for a better rspec writing: “Pro tips for writing better rspec tests” written by Josh Software. These giidences are quite easy to realize and follow. Sample codes are also provided as reference.
There are ten tips as follows:
  1. Follow Rspec style guide
  2. Independent tests
  3. Use expect and not should syntax
  4. Use the new syntax for object creation
  5. Use let variables instead of instance variables
  6. Use correct expectations for a spec
  7. Use shared examples
  8. Use conditional hooks
  9. Profiling your examples
  10. Tagging
For the document “Rspec style” on Githib, it must be read and followed first. it is the basic guide for rspec test. I see no reason why you didn’t study this guide before starting to write rspec tests.

< Traditional Chinese 繁體中文>
撰寫 rspec 測試是一件大工程,對新手而言,必須有大量的練習才能夠熟悉它。我剛好找到有一篇如何撰寫好的rspce tests的文章: “寫好rspec測試的專業訣竅 (Pro tips for writing better rspec tests)” 由 Josh Software撰寫。這些指引是非常容易了結及遵循的,也包含參考用的範例程式碼。
文中提供十個訣竅:
  1. 遵循 Rspec樣式 指引
  2. 獨立測試
  3. 使用 expect 而非 should 語法
  4. 建立物件時使用新語法
  5. 使用 let 變數來取代實例變數(instance variable)
  6. 對規格使用正確的期待值
  7. 使用 shared examples
  8. 使用條件式掛勾
  9. 對 examples 加以剖析
  10. 標記
對於在 Github 上的 Rspec style 文件,我們必須先要詳讀以及遵循,它是 rspec test 的基本指引。我覺得在你開始撰寫 rspec test 之前,沒有理由不先詳加閱讀。