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
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
110
Building an Accessibility Culture, One Step at a Time
basthomas
1
93
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
Vueで学ぶデータ構造入門 リンクリストとキューでリアクティビティを捉える / Vue Data Structures: Linked Lists and Queues for Reactivity
konkarin
1
350
Micro Frontendsで築いた 共通基盤と運用の試行錯誤 / Building a Shared Platform with Micro Frontends: Operational Learnings
kyntk
0
1.6k
TypeScript 5.9 で使えるようになった import defer でパフォーマンス最適化を実現する
bicstone
1
510
GraalVM Native Image トラブルシューティング機能の最新状況(2025年版)
ntt_dsol_java
0
170
モビリティSaaSにおけるデータ利活用の発展
nealle
1
650
チーム開発の “地ならし"
konifar
8
6.2k
GeistFabrik and AI-augmented software development
adewale
PRO
0
200
データファイルをAWSのDWHサービスに格納する / 20251115jawsug-tochigi
kasacchiful
2
100
これだけで丸わかり!LangChain v1.0 アップデートまとめ
os1ma
4
190
分散DBって何者なんだ... Spannerから学ぶRDBとの違い
iwashi623
0
120
AIの弱点、やっぱりプログラミングは人間が(も)勉強しよう / YAPC AI and Programming
kishida
13
5.4k
最新のDirectX12で使えるレイトレ周りの機能追加について
projectasura
0
300
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
55
12k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
11
940
jQuery: Nuts, Bolts and Bling
dougneiner
65
8k
How to Think Like a Performance Engineer
csswizardry
28
2.3k
Typedesign – Prime Four
hannesfritz
42
2.9k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.8k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.2k
Designing for humans not robots
tammielis
254
26k
For a Future-Friendly Web
brad_frost
180
10k
How to train your dragon (web standard)
notwaldorf
97
6.4k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.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