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
82
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
69
Building an Accessibility Culture, One Step at a Time
basthomas
1
57
Building a modern subscription experience on iOS
basthomas
0
150
Not an afterthought: accessibility from start to finish
basthomas
0
99
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
86
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
120
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
96
Effective Pull Request Reviews
basthomas
0
360
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
93
Other Decks in Programming
See All in Programming
Djangoアプリケーション 運用のリアル 〜問題発生から可視化、最適化への道〜 #pyconshizu
kashewnuts
1
230
一休.com のログイン体験を支える技術 〜Web Components x Vue.js 活用事例と最適化について〜
atsumim
0
110
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2.2k
SRE、開発、QAが協業して挑んだリリースプロセス改革@SRE Kaigi 2025
nealle
3
4.1k
ARA Ansible for the teams
kksat
0
150
Kanzawa.rbのLT大会を支える技術の裏側を変更する Ruby on Rails + Litestream 編
muryoimpl
0
220
ASP. NET CoreにおけるWebAPIの最新情報
tomokusaba
0
360
sappoRo.R #12 初心者セッション
kosugitti
0
230
2,500万ユーザーを支えるSREチームの6年間のスクラムのカイゼン
honmarkhunt
6
5.1k
[Fin-JAWS 第38回 ~re:Invent 2024 金融re:Cap~]FaultInjectionServiceアップデート@pre:Invent2024
shintaro_fukatsu
0
400
Open source software: how to live long and go far
gaelvaroquaux
0
620
Immutable ActiveRecord
megane42
0
130
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
A Tale of Four Properties
chriscoyier
158
23k
Docker and Python
trallard
44
3.3k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Become a Pro
speakerdeck
PRO
26
5.1k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
Designing Experiences People Love
moore
139
23k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
Adopting Sorbet at Scale
ufuk
74
9.2k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
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