Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Writing Testable Code
Search
Bas Broek
November 28, 2019
Programming
0
94
Writing Testable Code
Workshop at The Swift Alps 2019
Bas Broek
November 28, 2019
Tweet
Share
More Decks by Bas Broek
See All by Bas Broek
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
88
Building an Accessibility Culture, One Step at a Time
basthomas
1
73
Building a modern subscription experience on iOS
basthomas
0
160
Not an afterthought: accessibility from start to finish
basthomas
0
120
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
110
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
140
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
120
Effective Pull Request Reviews
basthomas
0
390
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
110
Other Decks in Programming
See All in Programming
Bedrock × Confluenceで簡単(?)社内RAG
iharuoru
1
120
音声プラットフォームのアーキテクチャ変遷から学ぶ、クラウドネイティブなバッチ処理 (20250422_CNDS2025_Batch_Architecture)
thousanda
0
410
The Missing Link in Angular’s Signal Story: Resource API and httpResource
manfredsteyer
PRO
0
140
Storybookの情報をMCPサーバー化する
shota_tech
2
920
Optimizing JRuby 10
headius
0
580
VitestのIn-Source Testingが便利
taro28
8
2.4k
Vibe Coding の話をしよう
schroneko
14
3.8k
20250426 GDGoC 合同新歓 - GDGoC のススメ
getty708
0
110
iOSアプリで測る!名古屋駅までの 方向と距離
ryunakayama
0
160
ぽちぽち選択するだけでOSSを読めるVSCode拡張機能
ymbigo
14
6.2k
複雑なフォームの jotai 設計 / Designing jotai(state) for Complex Forms #layerx_frontend
izumin5210
6
1.5k
AIコーディングの理想と現実
tomohisa
36
39k
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
53
11k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Code Reviewing Like a Champion
maltzj
523
40k
Fireside Chat
paigeccino
37
3.4k
The Cult of Friendly URLs
andyhume
78
6.3k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
13
840
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
How GitHub (no longer) Works
holman
314
140k
Speed Design
sergeychernyshev
29
930
Done Done
chrislema
184
16k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Transcript
Writing Testable Code 1 — @basthomas
Tests, what are they good for? 2 — @basthomas
What do you want to verify? 3 — @basthomas
How do you want to verify it? 4 — @basthomas
Dependency injection: three ways 5 — @basthomas
class Network { let urlSession: URLSession init(urlSession: URLSession = .shared)
{ self.urlSession = urlSession } } 6 — @basthomas
class Network { var urlSession = URLSession.shared } 7 —
@basthomas
class Network { func request(using urlSession: URLSession = .shared) {
} } 8 — @basthomas
Mocking through Protocols 9 — @basthomas
protocol UIApplicationProtocol { func canOpenURL(_ url: URL) -> Bool func
open( _ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any], completionHandler completion: ((Bool) -> Void)? ) } extension UIApplication: UIApplicationProtocol {} 10 — @basthomas
Mocking through Subclassing 11 — @basthomas
class URLSessionDataTaskMock: URLSessionDataTask { private let closure: () -> Void
init(closure: @escaping () -> Void) { self.closure = closure } override func resume() { closure() } } 12 — @basthomas
class URLSessionMock: URLSession { var data: Data? var error: Error?
override func dataTask( with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void ) -> URLSessionDataTask { let data = self.data let error = self.error return URLSessionDataTaskMock { completionHandler(data, nil, error) } } } 13 — @basthomas
Custom asserts 14 — @basthomas
func assertManyThings( cookie: Cookie, file: StaticString = #file, line: UInt
= #line ) { XCTAssertTrue(cookie.isDoughy, file: file, line: line) XCTAssertLessThan(cookie.calories, 200, file: file, line: line) } 15 — @basthomas
XCTest 16 — @basthomas
XCTUnwrap XCTNoThrow 17 — @basthomas
Let's talk about "flaky" 18 — @basthomas
19 — @basthomas
Make sure your architecture takes testability into account 20 —
@basthomas
Tests, Code, Documentation 21 — @basthomas
Treat your test code like your real code 22 —
@basthomas
Reviews of tests, not reviews with tests 23 — @basthomas
"Your" turn: let's take a look at something you want
to have covered with tests 24 — @basthomas