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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
39
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
210
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
[KCD Czech] eBPF Meets the GPU: Future of AI Infra Observability
doniacld
0
120
oxlintはeslint/typescript-eslintを置き換えられるのか
shomafujita
2
280
[BalkanRuby 2026] Drop your app/services!
palkan
3
720
Oxlintのカスタムルールの現況
syumai
5
860
Signal Forms: Beyond the Basics @ngBaguette 2026 in Paris
manfredsteyer
PRO
0
160
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
320
AI Agent と正しく分析するための環境作り
yoshyum
3
630
Spec-Driven Development with AI-Agents: From High-Level Requirements to Working Software
antonarhipov
2
380
1人1案件のプロダクトエンジニア時代に、"プロセス監督"としてチャレンジしたこと
non0113
0
350
AI 時代のソフトウェア設計の学び方
masuda220
PRO
28
11k
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
11
3k
自動レビューエンジンの実装と運用 ~レビューのない世界へ~
kurukuru1999
2
280
Featured
See All Featured
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
590
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Navigating Team Friction
lara
192
16k
Everyday Curiosity
cassininazir
0
210
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Faster Mobile Websites
deanohume
310
31k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
Design in an AI World
tapps
1
220
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
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