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
67
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
130
Not an afterthought: accessibility from start to finish
basthomas
0
78
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
59
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
100
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
72
Effective Pull Request Reviews
basthomas
0
330
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
65
An Introduction to Unit Testing Logic (in Swift)
basthomas
0
75
Swift 5's Custom String Interpolation in Practice
basthomas
0
430
Other Decks in Programming
See All in Programming
CSC307 Lecture 07
javiergs
PRO
0
220
生成AIをkintoneに連携してみた
hideg
0
230
CSC307 Lecture 05
javiergs
PRO
0
210
英語
s_shimotori
1
220
AWS初心者ってどうやってAWSを学ぶ?〜アプリエンジニアがやってよかったアーキテクチャ学習方法〜
yamanashi_ren01
0
190
[After Kotlin Fest 2024 LT Night @ Sansan] もっともっとKotlinを好きになる!K2 Compiler Pluginで遊んでみよう!
kitakkun
2
260
【Go言語】golangci-lintの使い方
tomo1227
0
270
Ruby メモリ管理 プログラミング
megmogmog1965
0
130
GraphQL はいいぞ! ~Laravel で学ぶ GraphQL 入門~
azuki
1
160
Microservices rules (July 2024) : what good looks like
cer
PRO
0
1.6k
君たちはどうコードをレビューする (される) か / 大吉祥寺.pm
utgwkk
15
8.5k
「2024年版 Kotlin サーバーサイドプログラミング実践開発」の補講 〜O/Rマッパー編〜
n_takehata
2
260
Featured
See All Featured
Web Components: a chance to create the future
zenorocha
307
41k
Fontdeck: Realign not Redesign
paulrobertlloyd
79
5.1k
Code Reviewing Like a Champion
maltzj
517
39k
Building Applications with DynamoDB
mza
89
5.8k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
26
1.6k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
277
13k
Faster Mobile Websites
deanohume
303
30k
YesSQL, Process and Tooling at Scale
rocio
166
14k
Learning to Love Humans: Emotional Interface Design
aarron
269
39k
Designing on Purpose - Digital PM Summit 2013
jponch
113
6.6k
Done Done
chrislema
179
15k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
35
6.3k
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