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
73
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
45
Building an Accessibility Culture, One Step at a Time
basthomas
1
40
Building a modern subscription experience on iOS
basthomas
0
130
Not an afterthought: accessibility from start to finish
basthomas
0
81
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
64
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
100
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
79
Effective Pull Request Reviews
basthomas
0
340
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
76
Other Decks in Programming
See All in Programming
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
200
Quine, Polyglot, 良いコード
qnighy
4
650
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
250
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
最新TCAキャッチアップ
0si43
0
200
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
5
300
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
230
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Faster Mobile Websites
deanohume
305
30k
Why Our Code Smells
bkeepers
PRO
334
57k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Docker and Python
trallard
40
3.1k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
430
Designing for Performance
lara
604
68k
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