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
160
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Writing Testable Code
Workshop at The Swift Alps 2019
Bas Broek
November 28, 2019
More Decks by Bas Broek
See All by Bas Broek
Assistive Access: Reinventing Your App for a New (sort of) Generation
basthomas
0
25
Roasting Your App's Accessibility
basthomas
0
60
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
150
Building an Accessibility Culture, One Step at a Time
basthomas
1
130
Building a modern subscription experience on iOS
basthomas
0
230
Not an afterthought: accessibility from start to finish
basthomas
0
180
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
200
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
200
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
200
Other Decks in Programming
See All in Programming
Go 1.27 における memory allocation の高速化
andpad
0
360
Press start. Python's next generation.
willingc
PRO
3
290
Flow は今どうなっているか
mizdra
PRO
0
790
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
560
私のClaude Code活用法 (個人開発編) - PHPerKaigi mini #4(2026/08/24)
panda_program
1
190
楽しそうなつよつよエンジニアと目が死んでる僕/A brilliant engineer having a blast, and dead-eyed me.
3l4l5
2
270
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.8k
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
370
T3DD26: From RAGs to Riches
martinhelmich
0
120
AIに既存システムを理解させる技術 ~レガシーを見捨てないハーネスエンジニアリング入門~
ochtum
0
180
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
470
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
280
Featured
See All Featured
Mind Mapping
helmedeiros
PRO
1
340
Ethics towards AI in product and experience design
skipperchong
2
350
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
680
Color Theory Basics | Prateek | Gurzu
gurzu
0
450
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Are puppies a ranking factor?
jonoalderson
2
3.9k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
390
KATA
mclloyd
PRO
35
15k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Un-Boring Meetings
codingconduct
0
400
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
240
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