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
100
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
99
Building an Accessibility Culture, One Step at a Time
basthomas
1
78
Building a modern subscription experience on iOS
basthomas
0
170
Not an afterthought: accessibility from start to finish
basthomas
0
120
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
120
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
150
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
130
Effective Pull Request Reviews
basthomas
0
400
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
120
Other Decks in Programming
See All in Programming
Benchmark
sysong
0
270
エンジニア向け採用ピッチ資料
inusan
0
160
既存デザインを変更せずにタップ領域を広げる方法
tahia910
1
240
XP, Testing and ninja testing
m_seki
3
200
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
320
プロダクト志向ってなんなんだろうね
righttouch
PRO
0
160
NPOでのDevinの活用
codeforeveryone
0
380
High-Level Programming Languages in AI Era -Human Thought and Mind-
hayat01sh1da
PRO
0
470
ASP.NETアプリケーションのモダナイズ インフラ編
tomokusaba
1
420
Result型で“失敗”を型にするPHPコードの書き方
kajitack
4
480
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
200
PHPでWebSocketサーバーを実装しよう2025
kubotak
0
210
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
Building Applications with DynamoDB
mza
95
6.5k
Rails Girls Zürich Keynote
gr2m
94
14k
Practical Orchestrator
shlominoach
188
11k
Making the Leap to Tech Lead
cromwellryan
134
9.3k
Bash Introduction
62gerente
614
210k
We Have a Design System, Now What?
morganepeng
53
7.7k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
How to Think Like a Performance Engineer
csswizardry
24
1.7k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Code Review Best Practice
trishagee
68
18k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
124
52k
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