Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
4
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
120
Building an Accessibility Culture, One Step at a Time
basthomas
1
96
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
160
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
160
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
160
Effective Pull Request Reviews
basthomas
0
420
Other Decks in Programming
See All in Programming
TestingOsaka6_Ozono
o3
0
170
実はマルチモーダルだった。ブラウザの組み込みAI🧠でWebの未来を感じてみよう #jsfes #gemini
n0bisuke2
3
1.3k
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
420
UIデザインに役立つ 2025年の最新CSS / The Latest CSS for UI Design 2025
clockmaker
18
7.6k
【CA.ai #3】ワークフローから見直すAIエージェント — 必要な場面と“選ばない”判断
satoaoaka
0
270
実は歴史的なアップデートだと思う AWS Interconnect - multicloud
maroon1st
0
240
まだ間に合う!Claude Code元年をふりかえる
nogu66
5
870
從冷知識到漏洞,你不懂的 Web,駭客懂 - Huli @ WebConf Taiwan 2025
aszx87410
2
2.9k
Flutter On-device AI로 완성하는 오프라인 앱, 박제창 @DevFest INCHEON 2025
itsmedreamwalker
1
140
tparseでgo testの出力を見やすくする
utgwkk
2
260
公共交通オープンデータ × モバイルUX 複雑な運行情報を 『直感』に変換する技術
tinykitten
PRO
0
150
「コードは上から下へ読むのが一番」と思った時に、思い出してほしい話
panda728
PRO
39
26k
Featured
See All Featured
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
130
Google's AI Overviews - The New Search
badams
0
860
The Cost Of JavaScript in 2023
addyosmani
55
9.4k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
0
89
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
40
Rails Girls Zürich Keynote
gr2m
95
14k
Discover your Explorer Soul
emna__ayadi
2
1k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
0
1.7k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
0
170
Java REST API Framework Comparison - PWX 2021
mraible
34
9k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
64
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
280
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