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
84
Building a modern subscription experience on iOS
basthomas
0
170
Not an afterthought: accessibility from start to finish
basthomas
0
130
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
130
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
150
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
130
Effective Pull Request Reviews
basthomas
0
400
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
140
Other Decks in Programming
See All in Programming
Dart 参戦!!静的型付き言語界の隠れた実力者
kno3a87
0
190
Flutterと Vibe Coding で個人開発!
hyshu
1
250
大規模FlutterプロジェクトのCI実行時間を約8割削減した話
teamlab
PRO
0
470
AHC051解法紹介
eijirou
0
470
Claude Code と OpenAI o3 で メタデータ情報を作る
laket
0
130
新しいモバイルアプリ勉強会(仮)について
uetyo
1
250
AIに安心して任せるためにTypeScriptで一意な型を作ろう
arfes0e2b3c
0
340
Portapad紹介プレゼンテーション
gotoumakakeru
1
130
ZeroETLで始めるDynamoDBとS3の連携
afooooil
0
160
Go製CLIツールをnpmで配布するには
syumai
2
1.2k
ゲームの物理
fadis
5
1.1k
CEDEC2025 長期運営ゲームをあと10年続けるための0から始める自動テスト ~4000項目を50%自動化し、月1→毎日実行にした3年間~
akatsukigames_tech
0
120
Featured
See All Featured
Gamification - CAS2011
davidbonilla
81
5.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Raft: Consensus for Rubyists
vanstee
140
7.1k
Testing 201, or: Great Expectations
jmmastey
45
7.6k
Statistics for Hackers
jakevdp
799
220k
Documentation Writing (for coders)
carmenintech
73
5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
47
9.6k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.3k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Faster Mobile Websites
deanohume
309
31k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.8k
Making the Leap to Tech Lead
cromwellryan
134
9.5k
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