Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Writing Testable Code
Search
Bas Broek
November 28, 2019
Programming
170
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Writing Testable Code
Workshop at The Swift Alps 2019
Bas Broek
November 28, 2019
More Decks by Bas Broek
See All by Bas Broek
Assistive Access: Reinventing Your App for a New (sort of) Generation
basthomas
0
36
Roasting Your App's Accessibility
basthomas
0
69
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
170
Building an Accessibility Culture, One Step at a Time
basthomas
1
140
Building a modern subscription experience on iOS
basthomas
0
240
Not an afterthought: accessibility from start to finish
basthomas
0
200
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
210
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
210
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
210
Other Decks in Programming
See All in Programming
[ハンズオン]AIへの指示だけで「五目並べ」を作ってみよう
satoshi256kbyte
1
380
Domain-Driven Transformation
hschwentner
2
2.3k
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
430
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
sbt 2.0: takeoff
eed3si9n
0
160
Rでドレミ/Do-Re-Mi_with_R
florets1
0
130
ActionController meets Protocol Buffers
sorah
0
550
パズルゲームの作り方 / how to make puzzle games
kaityo256
PRO
2
300
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
280
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
270
Performance Optimization With LLMs
xleitix
0
130
最近”作らせてる” ローカルAIアプリの話
akit37
0
480
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.2k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
640
How GitHub (no longer) Works
holman
316
150k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
630
For a Future-Friendly Web
brad_frost
184
10k
Test your architecture with Archunit
thirion
2
2.4k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
How Software Deployment tools have changed in the past 20 years
geshan
2
35k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
600
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