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
20
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
130
Building an Accessibility Culture, One Step at a Time
basthomas
1
100
Building a modern subscription experience on iOS
basthomas
0
190
Not an afterthought: accessibility from start to finish
basthomas
0
150
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
160
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
170
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
170
Effective Pull Request Reviews
basthomas
0
430
Other Decks in Programming
See All in Programming
Codex の「自走力」を高める
yorifuji
0
230
エージェント開発初心者の僕がエージェントを作った話と今後やりたいこと
thasu0123
0
230
Go1.26 go fixをプロダクトに適用して困ったこと
kurakura0916
0
320
AIとペアプロして処理時間を97%削減した話 #pyconshizu
kashewnuts
1
200
encoding/json/v2のUnmarshalはこう変わった:内部実装で見る設計改善
kurakura0916
0
310
TipKitTips
ktcryomm
0
150
CSC307 Lecture 12
javiergs
PRO
0
460
今、アーキテクトとして 品質保証にどう関わるか
nealle
0
200
20260228_JAWS_Beginner_Kansai
takuyay0ne
5
440
「やめとこ」がなくなった — 1月にZennを始めて22本書いた AI共創開発のリアル
atani14
0
350
AIに任せる範囲を安全に広げるためにやっていること
fukucheee
0
110
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
200
Featured
See All Featured
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
760
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Crafting Experiences
bethany
1
75
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
68
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.7k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
2.3k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
150
The Curious Case for Waylosing
cassininazir
0
260
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.1k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
80
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