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
140
0
Share
Writing Testable Code
Workshop at The Swift Alps 2019
Bas Broek
November 28, 2019
More Decks by Bas Broek
See All by Bas Broek
Roasting Your App's Accessibility
basthomas
0
26
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
170
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
Laravel Nightwatchの裏側 - Laravel公式Observabilityツールを支える設計と実装
avosalmon
1
300
見せてもらおうか、 OpenSearchの性能とやらを!
shunta27
1
170
Java 21/25 Virtual Threads 소개
debop
0
320
S3ストレージクラスの「見える」「ある」「使える」は全部違う ─ 体験から見た、仕様の深淵を覗く
ya_ma23
0
1.2k
GC言語のWasm化とComponent Modelサポートの実践と課題 - Scalaの場合
tanishiking
0
140
Codex CLIのSubagentsによる並列API実装 / Parallel API Implementation with Codex CLI Subagents
takatty
2
760
KagglerがMixSeekを触ってみた
morim
0
360
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
160
モダンOBSプラグイン開発
umireon
0
190
「効かない!」依存性注入(DI)を活用したAPI Platformのエラーハンドリング奮闘記
mkmk884
0
290
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
250
AWS×クラウドネイティブソフトウェア設計 / AWS x Cloud-Native Software Design
nrslib
16
3.5k
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Darren the Foodie - Storyboard
khoart
PRO
3
3.1k
Faster Mobile Websites
deanohume
310
31k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
170
New Earth Scene 8
popppiees
2
2k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
290
RailsConf 2023
tenderlove
30
1.4k
For a Future-Friendly Web
brad_frost
183
10k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
500
A designer walks into a library…
pauljervisheath
211
24k
Evolving SEO for Evolving Search Engines
ryanjones
0
170
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.4k
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