Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Writing Testable Code
Bas Broek
November 28, 2019
Programming
0
47
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 a modern subscription experience on iOS
basthomas
0
59
Not an afterthought: accessibility from start to finish
basthomas
0
22
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
30
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
44
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
44
Effective Pull Request Reviews
basthomas
0
260
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
43
An Introduction to Unit Testing Logic (in Swift)
basthomas
0
50
Swift 5's Custom String Interpolation in Practice
basthomas
0
270
Other Decks in Programming
See All in Programming
ipa-medit: Memory search and patch tool for IPA without Jailbreaking/ipa-medit-bh2022-europe
tkmru
0
130
Remix + Cloudflare Pages + D1 で ポケモン SV のレンタルチームを検索できるアプリを作ってみた
kuroppe1819
4
1.4k
ECテックカンファレンス2023
kspace
1
380
PHP でガチの電卓を作る
memory1994
PRO
2
160
エンジニア向け会社紹介資料/engineer-recruiting-pitch
xmile
PRO
0
120
xarray-Datatree: Hierarchical Data Structures for Multi-Model Science
tomnicholas
0
230
Enumを自動で網羅的にテストしてみた
estie
0
1.4k
そうだ、10Gを引こう
mattenn
0
100
Hono v3 - Do Everything, Run Anywhere, But Small, And Faster
yusukebe
4
140
Remote SSHで行うVS Codeリモートホスト開発とトラブルシューティング
smt7174
1
520
Qiita Night PHP 2023
fuwasegu
0
11k
23年のJavaトレンドは?Quarkusで理解するコンテナネイティブJava
tatsuya1bm
1
140
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
7
590
The Illustrated Children's Guide to Kubernetes
chrisshort
22
43k
YesSQL, Process and Tooling at Scale
rocio
159
12k
Git: the NoSQL Database
bkeepers
PRO
419
60k
Become a Pro
speakerdeck
PRO
6
3.2k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
29
8k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
6
850
Principles of Awesome APIs and How to Build Them.
keavy
117
15k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
31
20k
Bootstrapping a Software Product
garrettdimon
299
110k
Designing for humans not robots
tammielis
245
24k
The MySQL Ecosystem @ GitHub 2015
samlambert
240
11k
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