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
28
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
130
Building an Accessibility Culture, One Step at a Time
basthomas
1
100
Building a modern subscription experience on iOS
basthomas
0
190
Not an afterthought: accessibility from start to finish
basthomas
0
150
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
170
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
170
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
170
Effective Pull Request Reviews
basthomas
0
430
Other Decks in Programming
See All in Programming
20260315 AWSなんもわからん🥲
chiilog
2
190
Strategy for Finding a Problem for OSS: With Real Examples
kibitan
0
140
メッセージングを利用して時間的結合を分離しよう #phperkaigi
kajitack
3
560
煩雑なSkills管理をSoC(関心の分離)により解決する――関心を分離し、プロンプトを部品として育てるためのOSSを作った話 / Solving Complex Skills Management Through SoC (Separation of Concerns)
nrslib
3
540
感情を設計する
ichimichi
5
1.2k
Symfonyの特性(設計思想)を手軽に活かす特性(trait)
ickx
0
130
実践CRDT
tamadeveloper
0
260
生成 AI 時代のスナップショットテストってやつを見せてあげますよ(α版)
ojun9
0
340
夢の無限スパゲッティ製造機 -実装篇- #phpstudy
o0h
PRO
0
190
へんな働き方
yusukebe
6
2.9k
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
230
脱 雰囲気実装!AgentCoreを良い感じにWEBアプリケーションに組み込むために
takuyay0ne
3
440
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
The Limits of Empathy - UXLibs8
cassininazir
1
290
Docker and Python
trallard
47
3.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
170
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
310
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
800
Faster Mobile Websites
deanohume
310
31k
Optimizing for Happiness
mojombo
378
71k
The untapped power of vector embeddings
frankvandijk
2
1.7k
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