Tdd with rspec.md

Preview:

Citation preview

TDD with RSpec● TDD● RSpec● Build a Markdown editor

TDD?Test-Driven Development, 測試驅動開發

● 開發方法

● spec (specification 規格)確認的設計活動

● 快速取得回饋

● 限定變動在可以掌握的範圍內

TDD 是明確且可執行的規格

● 把 test 當作 spec● 可以執行,也能讓人閱讀

TDD 用規格杜絕浪費

● 等待與搬運的浪費:前後端可以同步開發

● 不良品的浪費:明確定義什麼是正確的,因為可以被執行,所以可以隨時檢查有

無錯誤產生

● 動作與加工的浪費:可以很方便的進行小部分功能開發驗證

● 製造過多或過早的浪費:不會發散開發出規格以外的功能

TDD 兩頂帽子

● 實現功能的帽子:在測試的輔助下,快速實現其功能。

● 重構的帽子:在測試的保護下,通過去除多餘的程式,提高程式質量

TDD 三個階段

紅燈、綠燈、重構 Red, Green, Refactor

在還沒有主要功能之前,先寫單元測試。由於主要功能都還沒有撰寫,自然無法通過

剛剛寫出來的單元測試,所以會亮出紅色的燈號。

快速實作主要功能,直到可以通過單元測試,讓測試的燈號變成綠色。

若我們需要更大的彈性,就整理程式碼,消除重覆設計。並保証功能仍然綠燈可運

行。

Why RSpec? We use Ruby on Rails

What is RSpec?Test framework for Ruby

Let's TDD with RSpec 小步快跑!

99 Bottles �https://www.youtube.com/watch?v=Xy-da43E6Lo

99 Bottles describe "#verse" doend

describe "#verse" do context "99" do endend

describe "#verse" do context "99" do

expected = <<-VERSE99 bottles of beer on the wall, 99 bottles of beer.Take one down and pass it around, 98 bottles of beer on the wall. VERSE

expect(expected).to eq(Bottles.new.verse(99)) endend

99 Bottles → class Bottles def verse(number) "99 bottles of beer on the wall, 99 bottles of beer.\n" \ "Take one down and pass it around, 98 bottles of beer on the wall.\n" endend

99 Bottles → → (♻�) → → ...

99 Bottles 測試 → 開發 → (重構) → 測試 → 開發 ...more... http://bit.ly/2koOVLU

Live Demo

Your turn!

Your turn!

Your turn! ♻�

Next: TDD by User Story

時時勤拂拭,何處惹塵埃

TDD style: 規格明確,杜絕浪費,小步快跑

Bug 退散!