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
89
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
83
Building an Accessibility Culture, One Step at a Time
basthomas
1
71
Building a modern subscription experience on iOS
basthomas
0
160
Not an afterthought: accessibility from start to finish
basthomas
0
110
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
110
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
140
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
110
Effective Pull Request Reviews
basthomas
0
380
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
110
Other Decks in Programming
See All in Programming
タイムゾーンの奥地は思ったよりも闇深いかもしれない
suguruooki
1
580
Memory API : Patterns, Performance et Cas d'Utilisation
josepaumard
0
110
Youtube Lofier - Chrome拡張開発
ninikoko
0
2.4k
AIコーディングワークフローの試行 〜AIエージェント×ワークフローでの自動化を目指して〜
rkaga
2
3.5k
MCP調べてみました! / Exploring MCP
uhzz
2
2.2k
AWS で実現する安全な AI エージェントの作り方 〜 Bedrock Engineer の実装例を添えて 〜 / how-to-build-secure-ai-agents
gawa
8
730
自分のために作ったアプリが、グローバルに使われるまで / Indie App Development Lunch LT
pixyzehn
1
150
国漢文混用体からHolloまで
minhee
1
180
Deoptimization: How YJIT Speeds Up Ruby by Slowing Down / RubyKaigi 2025
k0kubun
0
550
PHP で学ぶ OAuth 入門
azuki
1
150
Going Structural with Named Tuples
bishabosha
0
200
Building Scalable Mobile Projects: Fast Builds, High Reusability and Clear Ownership
cyrilmottier
2
260
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
430
65k
Thoughts on Productivity
jonyablonski
69
4.6k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.8k
Rebuilding a faster, lazier Slack
samanthasiow
80
8.9k
Visualization
eitanlees
146
16k
Practical Orchestrator
shlominoach
186
10k
Making Projects Easy
brettharned
116
6.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.1k
Six Lessons from altMBA
skipperchong
27
3.7k
Why Our Code Smells
bkeepers
PRO
336
57k
Side Projects
sachag
452
42k
YesSQL, Process and Tooling at Scale
rocio
172
14k
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