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
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
550
自分ひとりから始められる生産性向上の取り組み #でぃーぷらすオオサカ
irof
8
2.6k
テストをしないQAエンジニアは何をしているか?
nealle
0
130
ファインディの テックブログ爆誕までの軌跡
starfish719
2
1.1k
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
4
1.3k
定理証明プラットフォーム lapisla.net
abap34
1
1.7k
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
770
JavaScriptツール群「UnJS」を5分で一気に駆け巡る!
k1tikurisu
10
1.8k
CNCF Project の作者が考えている OSS の運営
utam0k
5
690
Multi Step Form, Decentralized Autonomous Organization
pumpkiinbell
1
660
“あなた” の開発を支援する AI エージェント Bedrock Engineer / introducing-bedrock-engineer
gawa
11
1.8k
GitHub Actions × RAGでコードレビューの検証の結果
sho_000
0
240
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Building Applications with DynamoDB
mza
93
6.2k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
20
2.4k
The Cost Of JavaScript in 2023
addyosmani
47
7.3k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
540
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.2k
Designing Experiences People Love
moore
139
23k
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.5k
Optimising Largest Contentful Paint
csswizardry
34
3.1k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.6k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
A designer walks into a library…
pauljervisheath
205
24k
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