$30 off During Our Annual Pro Sale. View Details »
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
76
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
52
Building an Accessibility Culture, One Step at a Time
basthomas
1
43
Building a modern subscription experience on iOS
basthomas
0
140
Not an afterthought: accessibility from start to finish
basthomas
0
84
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
67
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
110
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
81
Effective Pull Request Reviews
basthomas
0
340
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
79
Other Decks in Programming
See All in Programming
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
1
610
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
0
440
talk-with-local-llm-with-web-streams-api
kbaba1001
0
140
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
150
社内活動の取り組み紹介 ~ スリーシェイクでこんな取り組みしてます ~
bells17
0
380
React CompilerとFine Grained Reactivityと宣言的UIのこれから / The next chapter of declarative UI
ssssota
7
3.2k
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
150
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
230
Cursorでアプリケーションの追加開発や保守をどこまでできるか試したら得るものが多かった話
drumnistnakano
0
270
物流システムにおけるリファクタリングとアーキテクチャの再構築 〜依存関係とモジュール分割の重要性〜
deeprain
1
360
.NET Conf 2024の振り返り
tomokusaba
0
190
.NET 9アプリをCGIとして レンタルサーバーで動かす
mayuki
1
750
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
94
17k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Side Projects
sachag
452
42k
Building Your Own Lightsaber
phodgson
103
6.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
880
Optimizing for Happiness
mojombo
376
70k
The Cost Of JavaScript in 2023
addyosmani
45
6.9k
RailsConf 2023
tenderlove
29
920
Six Lessons from altMBA
skipperchong
27
3.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Building Adaptive Systems
keathley
38
2.3k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
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