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
110
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
110
Building an Accessibility Culture, One Step at a Time
basthomas
1
85
Building a modern subscription experience on iOS
basthomas
0
170
Not an afterthought: accessibility from start to finish
basthomas
0
130
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
130
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
150
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
140
Effective Pull Request Reviews
basthomas
0
400
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
140
Other Decks in Programming
See All in Programming
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
3
310
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
3.2k
さようなら Date。 ようこそTemporal! 3年間先行利用して得られた知見の共有
8beeeaaat
3
1.5k
HTMLの品質ってなんだっけ? “HTMLクライテリア”の設計と実践
unachang113
4
2.9k
Namespace and Its Future
tagomoris
6
710
チームのテスト力を鍛える
goyoki
3
760
print("Hello, World")
eddie
2
530
Cache Me If You Can
ryunen344
2
3k
テストカバレッジ100%を10年続けて得られた学びと品質
mottyzzz
2
610
Flutter with Dart MCP: All You Need - 박제창 2025 I/O Extended Busan
itsmedreamwalker
0
150
そのAPI、誰のため? Androidライブラリ設計における利用者目線の実践テクニック
mkeeda
2
1.8k
Swift Updates - Learn Languages 2025
koher
2
490
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Bash Introduction
62gerente
615
210k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
810
Documentation Writing (for coders)
carmenintech
74
5k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
113
20k
Gamification - CAS2011
davidbonilla
81
5.4k
Navigating Team Friction
lara
189
15k
How to Ace a Technical Interview
jacobian
279
23k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Docker and Python
trallard
46
3.6k
Code Reviewing Like a Champion
maltzj
525
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