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
76
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
57
Building an Accessibility Culture, One Step at a Time
basthomas
1
46
Building a modern subscription experience on iOS
basthomas
0
140
Not an afterthought: accessibility from start to finish
basthomas
0
89
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
72
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
110
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
86
Effective Pull Request Reviews
basthomas
0
350
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
85
Other Decks in Programming
See All in Programming
useSyncExternalStoreを使いまくる
ssssota
6
1.3k
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
260
ドメインイベント増えすぎ問題
h0r15h0
2
400
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
290
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
110
103 Early Hints
sugi_0000
1
250
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
140
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
170
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
200
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
340
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Docker and Python
trallard
42
3.1k
4 Signs Your Business is Dying
shpigford
182
21k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
What's in a price? How to price your products and services
michaelherold
243
12k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
How GitHub (no longer) Works
holman
311
140k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Scaling GitHub
holman
459
140k
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