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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Bas Broek
November 28, 2019
Programming
160
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Writing Testable Code
Workshop at The Swift Alps 2019
Bas Broek
November 28, 2019
More Decks by Bas Broek
See All by Bas Broek
Assistive Access: Reinventing Your App for a New (sort of) Generation
basthomas
0
25
Roasting Your App's Accessibility
basthomas
0
60
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
160
Building an Accessibility Culture, One Step at a Time
basthomas
1
130
Building a modern subscription experience on iOS
basthomas
0
230
Not an afterthought: accessibility from start to finish
basthomas
0
190
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
200
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
200
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
200
Other Decks in Programming
See All in Programming
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
330
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
110
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
280
Hono + Inertia + React で LP を構築した話
oukayuka
2
200
Go を使い始めて 2 ヶ月の学び / My first two months with Go
contour_gara
0
420
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
0
170
MySQLとPostgreSQLって何が違うの?
akagami
PRO
0
160
XHTMLが残したもの
yosuke_furukawa
PRO
1
380
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
810
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.5k
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
720
Oxlintはいいぞ(続)
yug1224
1
540
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
エンジニアに許された特別な時間の終わり
watany
108
250k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
290
A designer walks into a library…
pauljervisheath
211
25k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
690
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.9k
Making Projects Easy
brettharned
120
6.7k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.6k
SEO for Brand Visibility & Recognition
aleyda
0
4.7k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
260
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