LCTestsExample
Unit test with XCTest
- 驗證回傳
- 驗證狀態改變
- 驗證互動
3A原則
Arrange: 初始化 Act: 呼叫要測試的方法 Assert: 驗證結果
或者 Given / When / Then
Naming
TDD
- 搞清楚spec
- 設計接口(皮)與驗證方式
- 最快方式填實作到綠燈
紅燈 > 綠燈 > 重構 loop
Unit test and Integration test
- 外部依賴 任何存取到外部可能變動的來源都算是 Integration test,因為你的Code 沒改卻因為來源變動而造成Test fail
- API
- NSUserDefaults
- File
- DB
- Singleton
- NSDate
解除依賴
依賴注入 (dependency injection)
建構子注入 (構造器注入)
建議注入 id 如果注入太多參數,可能已經違背單一職能原則
屬性注入
方法注入
(隔離框架) 把 Integration test 轉成 Unit test
OCMock
- Mock Method
- Mock NSUserDefault
- Mock Singleton
非同步測試
- Block
- NSNotification
- KVO
UI testing - LoginViewController
- 用 Category 暴露 private method