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
90
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
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
83
Building an Accessibility Culture, One Step at a Time
basthomas
1
71
Building a modern subscription experience on iOS
basthomas
0
160
Not an afterthought: accessibility from start to finish
basthomas
0
110
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
110
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
140
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
110
Effective Pull Request Reviews
basthomas
0
380
Accessibility in SwiftUI: Better Apps for Everyone
basthomas
0
110
Other Decks in Programming
See All in Programming
Coding Experience Cpp vs Csharp - meetup app osaka@9
harukasao
0
750
メモリウォールを超えて:キャッシュメモリ技術の進歩
kawayu
0
1.9k
複雑なフォームの jotai 設計 / Designing jotai(state) for Complex Forms #layerx_frontend
izumin5210
3
720
海外のアプリで見かけたかっこいいTransitionを真似てみる
shogotakasaki
1
170
AWS で実現する安全な AI エージェントの作り方 〜 Bedrock Engineer の実装例を添えて 〜 / how-to-build-secure-ai-agents
gawa
8
800
PHP で学ぶ OAuth 入門
azuki
1
190
Qiita Bash
mercury_dev0517
2
200
国漢文混用体からHolloまで
minhee
1
190
Ruby's Line Breaks
yui_knk
2
920
SQL Server ベクトル検索
odashinsuke
0
180
AIコーディングワークフローの試行 〜AIエージェント×ワークフローでの自動化を目指して〜
rkaga
2
3.6k
PHPで書いたAPIをGoに書き換えてみた 〜パフォーマンス改善の可能性を探る実験レポート〜
koguuum
0
150
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Building Flexible Design Systems
yeseniaperezcruz
329
38k
How to train your dragon (web standard)
notwaldorf
90
6k
Making the Leap to Tech Lead
cromwellryan
133
9.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Cult of Friendly URLs
andyhume
78
6.3k
Unsuck your backbone
ammeep
670
57k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
For a Future-Friendly Web
brad_frost
176
9.7k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Writing Fast Ruby
sferik
628
61k
A Tale of Four Properties
chriscoyier
158
23k
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