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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Bas Broek
November 28, 2019
Programming
140
0
Share
Writing Testable Code
Workshop at The Swift Alps 2019
Bas Broek
November 28, 2019
More Decks by Bas Broek
See All by Bas Broek
Roasting Your App's Accessibility
basthomas
0
35
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
130
Building an Accessibility Culture, One Step at a Time
basthomas
1
110
Building a modern subscription experience on iOS
basthomas
0
200
Not an afterthought: accessibility from start to finish
basthomas
0
160
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
180
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
180
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
180
Effective Pull Request Reviews
basthomas
0
440
Other Decks in Programming
See All in Programming
의존성 주입과 모듈화
fornewid
0
160
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
120
How We Benchmarked Quarkus: Patterns and anti-patterns
hollycummins
1
180
ハーネスエンジニアリングとは?
kinopeee
13
6.7k
Symfony AI in Action - SymfonyLive Berlin 2026
chr_hertel
1
110
JAWS-UG横浜 #100 祝・第100回スペシャルAWS は VPC レスの時代へ
maroon1st
0
210
mruby on C#: From VM Implementation to Game Scripting (RubyKaigi 2026)
hadashia
2
1.5k
PHPでローカル環境用のSSL/TLS証明書を発行することはできるのか? #phpconkagawa
akase244
0
310
クラウドネイティブなエンジニアに向ける Raycastの魅力と実際の活用事例
nealle
2
230
2026-04-15 Spring IO - I Can See Clearly Now
jonatan_ivanov
1
160
Terraform言語の静的解析 / static analysis of Terraform language
wata727
1
130
From Formal Specification to Property Based Test
ohbarye
0
680
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
96
14k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
130
New Earth Scene 8
popppiees
3
2.2k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
130
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
120
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
280
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
200
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
350
Side Projects
sachag
455
43k
The Cult of Friendly URLs
andyhume
79
6.9k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
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