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
17
Roasting Your App's Accessibility
basthomas
0
56
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
190
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
200
Other Decks in Programming
See All in Programming
メールのエイリアス機能を履き違えない
isshinfunada
0
240
How I Won Prize Money at a Hackathon Using Codex and Symphony Alpha
yasei_no_otoko
0
110
数百円から始めるRuby電子工作
tarosay
0
160
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
340
テーブルをDELETEした
yuzneri
0
150
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
230
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
270
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
150
「人を評価する AI」の設計と実装
ryoyanara
0
210
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
270
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.7k
What's New in Android 2026
veronikapj
0
260
Featured
See All Featured
Embracing the Ebb and Flow
colly
88
5.1k
Producing Creativity
orderedlist
PRO
348
40k
Statistics for Hackers
jakevdp
799
230k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
800
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
First, design no harm
axbom
PRO
2
1.3k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Automating Front-end Workflow
addyosmani
1369
210k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
210
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
430
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.8k
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