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
71
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
34
Building an Accessibility Culture, One Step at a Time
basthomas
1
34
Building a modern subscription experience on iOS
basthomas
0
130
Not an afterthought: accessibility from start to finish
basthomas
0
79
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
61
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
100
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
76
Effective Pull Request Reviews
basthomas
0
330
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
73
Other Decks in Programming
See All in Programming
文化が生産性を作る
jimpei
3
630
Piniaの現状と今後
waka292
2
520
ポケモンで考えるコミュニケーション / Communication Lessons from Pokémon
mackey0225
5
220
Competitionsだけじゃない! Kaggle Notebooks Grandmasterのすすめ
corochann
2
940
Why I Choose NetBeans for Jakarta EE
ivargrimstad
0
850
Kotlin Multiplatform at Stable and Beyond (Kotlin Vienna, October 2024)
zsmb
2
410
CSC509 Lecture 05
javiergs
PRO
0
180
画像でわかる北島直樹
naoki0917
0
110
/←このスケジュール表に立ち向かう フロントエンド開発戦略 / A front-end development strategy to tackle a single-slash schedule.
nrslib
1
430
Vertical Architectures for Scalable Angular Applications
manfredsteyer
PRO
0
210
(Deep|Web) Link support with expo-router
mrtry
0
190
2024-10-02 dev2next - Application Observability like you've never heard before
jonatan_ivanov
0
200
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
44
5.9k
Become a Pro
speakerdeck
PRO
24
4.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
[RailsConf 2023] Rails as a piece of cake
palkan
49
4.8k
How to Think Like a Performance Engineer
csswizardry
18
1k
Statistics for Hackers
jakevdp
796
220k
Why You Should Never Use an ORM
jnunemaker
PRO
53
9k
No one is an island. Learnings from fostering a developers community.
thoeni
19
2.9k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
130k
GraphQLの誤解/rethinking-graphql
sonatard
66
9.9k
Thoughts on Productivity
jonyablonski
67
4.3k
The Invisible Side of Design
smashingmag
297
50k
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