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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Bas Broek
November 28, 2019
Programming
0
130
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
19
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
160
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
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
210
クライアントワークでSREをするということ。あるいは事業会社におけるSREと同じこと・違うこと
nnaka2992
1
230
nilとは何か 〜interfaceの構造とnil!=nilから理解する〜 / Understanding nil in Go Interface Representation and Why nil != nil
kuro_kurorrr
3
1.5k
CSC307 Lecture 14
javiergs
PRO
0
440
Ruby x Terminal
a_matsuda
5
510
Swift ConcurrencyでよりSwiftyに
yuukiw00w
0
210
今、アーキテクトとして 品質保証にどう関わるか
nealle
0
190
AIプロダクト時代のQAエンジニアに求められること
imtnd
1
500
Go Conference mini in Sendai 2026 : Goに新機能を提案し実装されるまでのフロー徹底解説
yamatoya
0
470
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
2k
社内規程RAGの精度を73.3% → 100%に改善した話
oharu121
9
5.1k
AIによる開発の民主化を支える コンテキスト管理のこれまでとこれから
mulyu
3
2.2k
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
30 Presentation Tips
portentint
PRO
1
240
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
[SF Ruby Conf 2025] Rails X
palkan
2
800
Bash Introduction
62gerente
615
210k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
470
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.7k
Navigating Team Friction
lara
192
16k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
250
Marketing to machines
jonoalderson
1
5k
So, you think you're a good person
axbom
PRO
2
1.9k
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