如何在實務上使用TDD來開發 twmvc#12

Post on 14-Dec-2014

468 views 5 download

description

TDD 貌似不難,但在實務上能推行與貫徹的團隊卻是少之又少,甚至被視為是一種不切實際的開發方式。在這次分享中,將搭配 demo 來介紹幾個貫徹 TDD 所需要的重要概念, 包含: IoC Unit testing Web UI testing BDD 期望讓大家在未來的工作上,也可以透過 TDD 快樂地開發。 by twMVC http://mvc.tw

Transcript of 如何在實務上使用TDD來開發 twmvc#12

如何在實務上使用TDD來開發

Joey Chen(91)

Microsoft MVP 2010~2013

C#

共同作者, 書籍譯者

軟體工程, 測試, 重構, CI, TDD, BDD…

2

About 91

Web UI Testing

Refactoring

Unit Testing

BDD

Interface

第一式打到最後一式

20

Agenda

22

Record Scenarios

Replay and Assert

Export to C#

Auto testing in C#

Demo

33

Demo – Selenium IDE & C#

Selenium IDE

[Selenium IDE]Export to C#/WebDriver/MSTest

NuGet

Selenium WebDriver

Selenium WebDriver Support Classes

Reference

35

Demo – Extract Method

Demo – 建立Class

Demo – 單元測試

Visual Studio Extension

NuGet

App.Config

<unitTestProvider name="MsTest" />

Scenarios

Demo – Specflow

Demo – Extract Interface

Web UI testing (acceptance testing)

Unit testing

Test cases by scenarios

Depend on interface

Recap

73

PO:

Team:

PO:

PO:

Team:

帳號輸入 joey

密碼輸入 1234

登入成功

導到首頁 ~/Home/Index

PO & team – 登入成功

81

帳號輸入 joey

密碼輸入 abc

登入失敗

呈現錯誤訊息為 “帳號或密碼有誤”

PO & team – 登入失敗

82

Team:

Demo – Prototype confirm

PO:

Demo – Describe Scenarios

Demo – Selenium testing

Account 傳入 joey

Password 傳入 1234

結果

Controller 應為 Home

Action 應為 Index

登入成功

95

Account 傳入 joey

Password 傳入 abc

結果

ViewBag.Message 為帳號或密碼有誤

登入失敗

96

Demo – Controller’s testing

Mock framework - RhinoMocks

RhinoMocks

RhinoMocks

Demo – IoC & Stub

Mock framework

Without DI

DI framework – Unity.MVC4

Container.RegisterType<TFrom,TTo>

Demo – DI & MyAuthentication

Get Password from Data Source

Hash Password to Compare

IAccountDao的GetPassword方法,傳入”joey”,回傳

“Joey hashed password”

IHash的GetHashResult方法,傳入”1234”,回傳“Joey

hashed password”

結果

True

IAccountDao回傳與IHash結果相同

115

IAccountDao的GetPassword方法,傳入”joey”,回傳

“Joey hashed password”

IHash的GetHashResult方法,傳入”abc”,回傳“abc

hashed result”

結果

False

IAccountDao回傳與IHash結果不同

116

Demo – MyAuthentication BDD

實作 IAccountDao

MyAccountDao with hard-code

實作 IHash

MyHash with hard-code

Container.RegisterType

Demo – MyAccountDao & MyHash

需求

雛形

SeleniumTest

ControllerTest

IAuthentication

DI with MyAuthentication

ControllerTest

IAccountDaoIHash

DI with MyAuthentication

MyAuthenticationTesting

MyAccountDao

MyAuthenticationTesting

MyHash

你也可以!

Q & A

Thanks for your listening

137

謝謝各位

• 本投影片所包含的商標與文字皆屬原著作者所有。• 本投影片使用的圖片皆從網路搜尋。• 本著作係採用 Creative Commons 姓名標示-非商業性-相同方式分享 3.0 台灣 (中華民國) 授權條款授權。

h t t p : / / m v c . t w