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
70
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 a modern subscription experience on iOS
basthomas
0
130
Not an afterthought: accessibility from start to finish
basthomas
0
78
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
60
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
100
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
74
Effective Pull Request Reviews
basthomas
0
330
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
70
An Introduction to Unit Testing Logic (in Swift)
basthomas
0
77
Swift 5's Custom String Interpolation in Practice
basthomas
0
470
Other Decks in Programming
See All in Programming
Debugging: All you need to know (for simultaneous interpreting)
jmatsu
2
950
あなたのアプリ、ログはでてますか?あるいはログをだしてますか? (Funabashi.dev用 軽量版)
uzulla
2
130
Google Sign-inの移行から始めるCredential Manager活用
clockvoid
0
430
サーバーレスで負荷試験!Step Functions + Lambdaを使ったk6の分散実行
shuntakahashi
6
1.6k
Pythonで改めて考える「クラス(class)」の使いどころ
os1ma
7
2k
実践!難読化ガイド
mitchan
0
250
Android開発以外のAndroid開発経験の活かしどころ
konifar
2
1.1k
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
9
2.4k
Patched fetch did not work
quramy
4
420
Scala におけるコンパイラエラーとの付き合い方
chencmd
2
430
GraphQLとGigaViewer for Apps
numeroanddev
2
190
AndroidアプリのUIバリエーションをあの手この手で確認する / Check UI variations of Android apps by various means
tkmnzm
1
200
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
48
2.8k
The World Runs on Bad Software
bkeepers
PRO
64
11k
Making Projects Easy
brettharned
113
5.8k
Happy Clients
brianwarren
96
6.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
36
1.7k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
326
21k
Designing for humans not robots
tammielis
248
25k
Speed Design
sergeychernyshev
22
440
From Idea to $5000 a Month in 5 Months
shpigford
379
46k
How to name files
jennybc
75
98k
The Invisible Customer
myddelton
119
13k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
29
2.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