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
140
0
Share
Writing Testable Code
Workshop at The Swift Alps 2019
Bas Broek
November 28, 2019
More Decks by Bas Broek
See All by Bas Broek
Roasting Your App's Accessibility
basthomas
0
35
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
130
Building an Accessibility Culture, One Step at a Time
basthomas
1
110
Building a modern subscription experience on iOS
basthomas
0
200
Not an afterthought: accessibility from start to finish
basthomas
0
160
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
180
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
180
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
180
Effective Pull Request Reviews
basthomas
0
440
Other Decks in Programming
See All in Programming
第3木曜LT会 #28
tinykitten
PRO
0
120
UIの境界線をデザインする | React Tokyo #15 メイントーク
sasagar
2
420
Making the RBS Parser Faster
soutaro
0
660
CursorとClaudeCodeとCodexとOpenCodeを実際に比較してみた
terisuke
1
520
ハーネスエンジニアリングにどう向き合うか 〜ルールファイルを超えて開発プロセスを設計する〜 / How to approach harness engineering
rkaga
27
19k
2026_04_15_量子計算をパズルとして解く
hideakitakechi
0
130
AIと共に生きる技術選定 2026
sgash708
0
120
t *testing.T は どこからやってくるの?
otakakot
1
890
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
470
Don't Prompt Harder, Structure Better
kitasuke
0
810
【26新卒研修】OpenAPI/Swagger REST API研修
dip_tech
PRO
0
130
ソフトウェア設計の結合バランス #phperkaigi
kajitack
0
490
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Fireside Chat
paigeccino
42
3.9k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
280
The Curse of the Amulet
leimatthew05
1
12k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
120
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
110
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.9k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
180
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
110
Marketing to machines
jonoalderson
1
5.2k
We Are The Robots
honzajavorek
0
220
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.6k
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