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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Bas Broek
November 28, 2019
Programming
0
130
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
Roasting Your App's Accessibility
basthomas
0
19
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
160
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
Go Conference mini in Sendai 2026 : Goに新機能を提案し実装されるまでのフロー徹底解説
yamatoya
0
440
文字コードの話
qnighy
43
16k
Ruby x Terminal
a_matsuda
5
480
AIによる高速開発をどう制御するか? ガードレール設置で開発速度と品質を両立させたチームの事例
tonkotsuboy_com
7
2.6k
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
310
「ブロックテーマでは再現できない」は本当か?
inc2734
0
1.1k
CSC307 Lecture 13
javiergs
PRO
0
310
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
330
RAGでハマりがちな"Excelの罠"を、データの構造化で突破する
harumiweb
6
1.5k
生成AIを活用したソフトウェア開発ライフサイクル変革の現在値
hiroyukimori
PRO
0
140
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
440
ご飯食べながらエージェントが開発できる。そう、Agentic Engineeringならね。
yokomachi
1
270
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
280
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
850
Reality Check: Gamification 10 Years Later
codingconduct
0
2k
The SEO Collaboration Effect
kristinabergwall1
0
370
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.3k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
160
Joys of Absence: A Defence of Solitary Play
codingconduct
1
300
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
110
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
300
We Have a Design System, Now What?
morganepeng
55
8k
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