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
150
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
16
Roasting Your App's Accessibility
basthomas
0
55
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
150
Building an Accessibility Culture, One Step at a Time
basthomas
1
130
Building a modern subscription experience on iOS
basthomas
0
230
Not an afterthought: accessibility from start to finish
basthomas
0
180
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
200
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
190
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
200
Other Decks in Programming
See All in Programming
VibeCodingからAgenticWorkflowへ
starfish719
0
490
自動化したのに回らないテスト運用の壁ーAI時代の品質責任と生産性
mfunaki
0
130
5分で問診!Composer セキュリティ健康診断
codmoninc
0
1k
Go言語とトイモデルで学ぶTransformerの気持ち / fukuokago23-transformer
monochromegane
0
180
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
330
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
1
1.7k
実装をデザインガイドラインに追従させるための取り組み / 260731-dip-mosh-design-system
dachi023
0
460
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
520
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
280
AIが無かった頃の素敵な出会いの話
codmoninc
1
430
Android CLI
fornewid
0
220
Google Apps Script で Ruby を動かす
kawahara
0
240
Featured
See All Featured
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
GraphQLとの向き合い方2022年版
quramy
50
15k
Believing is Seeing
oripsolob
1
190
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
Prompt Engineering for Job Search
mfonobong
0
400
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
470
Documentation Writing (for coders)
carmenintech
77
5.4k
What's in a price? How to price your products and services
michaelherold
247
13k
Six Lessons from altMBA
skipperchong
29
4.4k
We Have a Design System, Now What?
morganepeng
55
8.3k
Google's AI Overviews - The New Search
badams
0
1.1k
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