Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
120
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
Roasting Your App's Accessibility
basthomas
0
3
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
120
Building an Accessibility Culture, One Step at a Time
basthomas
1
95
Building a modern subscription experience on iOS
basthomas
0
180
Not an afterthought: accessibility from start to finish
basthomas
0
140
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
150
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
160
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
150
Effective Pull Request Reviews
basthomas
0
410
Other Decks in Programming
See All in Programming
tparseでgo testの出力を見やすくする
utgwkk
2
210
UIデザインに役立つ 2025年の最新CSS / The Latest CSS for UI Design 2025
clockmaker
18
7.4k
How Software Deployment tools have changed in the past 20 years
geshan
0
29k
Cap'n Webについて
yusukebe
0
130
Context is King? 〜Verifiability時代とコンテキスト設計 / Beyond "Context is King"
rkaga
9
1.1k
FluorTracer / RayTracingCamp11
kugimasa
0
230
ViewファーストなRailsアプリ開発のたのしさ
sugiwe
0
450
手軽に積ん読を増やすには?/読みたい本と付き合うには?
o0h
PRO
1
170
AIコーディングエージェント(skywork)
kondai24
0
160
LLM Çağında Backend Olmak: 10 Milyon Prompt'u Milisaniyede Sorgulamak
selcukusta
0
120
MAP, Jigsaw, Code Golf 振り返り会 by 関東Kaggler会|Jigsaw 15th Solution
hasibirok0
0
230
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
380
Featured
See All Featured
Context Engineering - Making Every Token Count
addyosmani
9
500
Speed Design
sergeychernyshev
33
1.4k
For a Future-Friendly Web
brad_frost
180
10k
It's Worth the Effort
3n
187
29k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Testing 201, or: Great Expectations
jmmastey
46
7.8k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Build The Right Thing And Hit Your Dates
maggiecrowley
38
3k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Music & Morning Musume
bryan
46
7k
How to Ace a Technical Interview
jacobian
280
24k
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