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
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
13
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
120
Building an Accessibility Culture, One Step at a Time
basthomas
1
98
Building a modern subscription experience on iOS
basthomas
0
190
Not an afterthought: accessibility from start to finish
basthomas
0
140
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
160
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
160
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
160
Effective Pull Request Reviews
basthomas
0
420
Other Decks in Programming
See All in Programming
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
390
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
160
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
950
2026年 エンジニアリング自己学習法
yumechi
0
120
ELYZA_Findy AI Engineering Summit登壇資料_AIコーディング時代に「ちゃんと」やること_toB LLMプロダクト開発舞台裏_20251216
elyza
2
1.4k
2年のAppleウォレットパス開発の振り返り
muno92
PRO
0
190
ThorVG Viewer In VS Code
nors
0
760
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
160
高速開発のためのコード整理術
sutetotanuki
1
360
The Art of Re-Architecture - Droidcon India 2025
siddroid
0
170
CSC307 Lecture 02
javiergs
PRO
1
770
AI時代の認知負荷との向き合い方
optfit
0
120
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
Chasing Engaging Ingredients in Design
codingconduct
0
110
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
120
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
59
42k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
170
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
120
Leo the Paperboy
mayatellez
4
1.4k
Docker and Python
trallard
47
3.7k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.1k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
280
The Mindset for Success: Future Career Progression
greggifford
PRO
0
230
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