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
76
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
57
Building an Accessibility Culture, One Step at a Time
basthomas
1
46
Building a modern subscription experience on iOS
basthomas
0
140
Not an afterthought: accessibility from start to finish
basthomas
0
87
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
71
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
110
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
85
Effective Pull Request Reviews
basthomas
0
350
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
84
Other Decks in Programming
See All in Programming
Jakarta EE meets AI
ivargrimstad
0
230
Refactor your code - refactor yourself
xosofox
1
260
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
Zoneless Testing
rainerhahnekamp
0
120
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
690
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
120
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
210
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
220
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
Featured
See All Featured
BBQ
matthewcrist
85
9.4k
A Philosophy of Restraint
colly
203
16k
For a Future-Friendly Web
brad_frost
175
9.4k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
RailsConf 2023
tenderlove
29
940
Producing Creativity
orderedlist
PRO
341
39k
Code Review Best Practice
trishagee
65
17k
Six Lessons from altMBA
skipperchong
27
3.5k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Optimizing for Happiness
mojombo
376
70k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
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