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
110
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
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
110
Building an Accessibility Culture, One Step at a Time
basthomas
1
91
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
140
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
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
150
Other Decks in Programming
See All in Programming
組込みだけじゃない!TinyGo で始める無料クラウド開発入門
otakakot
2
380
Software Architecture
hschwentner
6
2.4k
社会人になっても趣味開発を続けたい! / traPavilion
mazrean
1
120
AkarengaLT vol.38
hashimoto_kei
1
130
はじめてのDSPy - 言語モデルを『プロンプト』ではなく『プログラミング』するための仕組み
masahiro_nishimi
4
17k
Leading Effective Engineering Teams in the AI Era
addyosmani
7
690
オンデバイスAIとXcode
ryodeveloper
0
370
contribution to astral-sh/uv
shunsock
0
570
Amazon ECS Managed Instances が リリースされた!キャッチアップしよう!! / Let's catch up Amazon ECS Managed Instances
cocoeyes02
0
120
Designing Repeatable Edits: The Architecture of . in Vim
satorunooshie
0
210
GitHub Copilotを使いこなせ!/mastering_github_copilot!
kotakageyama
2
600
Dive into Triton Internals
appleparan
0
380
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.5k
How to Think Like a Performance Engineer
csswizardry
27
2.2k
Agile that works and the tools we love
rasmusluckow
331
21k
The Cult of Friendly URLs
andyhume
79
6.6k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
192
56k
Producing Creativity
orderedlist
PRO
348
40k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
2
220
The Power of CSS Pseudo Elements
geoffreycrofte
80
6k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1k
[RailsConf 2023] Rails as a piece of cake
palkan
57
6k
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