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
130
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
Roasting Your App's Accessibility
basthomas
0
9
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
120
Building an Accessibility Culture, One Step at a Time
basthomas
1
97
Building a modern subscription experience on iOS
basthomas
0
190
Not an afterthought: accessibility from start to finish
basthomas
0
140
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
160
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
160
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
160
Effective Pull Request Reviews
basthomas
0
420
Other Decks in Programming
See All in Programming
SQL Server 2025 LT
odashinsuke
0
160
Claude Codeの「Compacting Conversation」を体感50%減! CLAUDE.md + 8 Skills で挑むコンテキスト管理術
kmurahama
1
760
なぜSQLはAIぽく見えるのか/why does SQL look AI like
florets1
0
250
dchart: charts from deck markup
ajstarks
3
960
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
7
4.4k
AIエージェントの設計で注意するべきポイント6選
har1101
6
3.1k
CSC307 Lecture 02
javiergs
PRO
1
760
AI Agent Dojo #4: watsonx Orchestrate ADK体験
oniak3ibm
PRO
0
130
TestingOsaka6_Ozono
o3
0
270
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
210
CSC307 Lecture 01
javiergs
PRO
0
670
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
130
Featured
See All Featured
Accessibility Awareness
sabderemane
0
35
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Building the Perfect Custom Keyboard
takai
2
670
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
0
280
Building Adaptive Systems
keathley
44
2.9k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
0
120
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
150
The Mindset for Success: Future Career Progression
greggifford
PRO
0
210
We Have a Design System, Now What?
morganepeng
54
8k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
260
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
GraphQLとの向き合い方2022年版
quramy
50
14k
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