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
Network Testing in Swift with DVR
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Sam Soffes
October 08, 2015
Programming
6.5k
0
Share
Network Testing in Swift with DVR
Walkthrough of how to use DVR and the design behind it. Given at Realm on 2015-10-08.
Sam Soffes
October 08, 2015
More Decks by Sam Soffes
See All by Sam Soffes
Starting a Mobile Design System
soffes
0
140
Building Functional Apps in Swift for iOS & OS X
soffes
5
13k
Go
soffes
2
620
Favorites
soffes
2
820
Seesaw — Under the Playground
soffes
2
420
Ubiquitously Connected to the World
soffes
0
230
API First, Then Apps
soffes
3
370
Building Realtime Apps
soffes
6
8k
Dispatch Awesome
soffes
8
950
Other Decks in Programming
See All in Programming
의존성 주입과 모듈화
fornewid
0
150
10年分の技術的負債、完済へ ― Claude Code主導のAI駆動開発でスポーツブルを丸ごとリプレイスした話
takuya_houshima
0
2.7k
「Linuxサーバー構築標準教科書」を読んでみた #ツナギメオフライン.7
akase244
0
1.4k
PCOVから学ぶコードカバレッジ #phpcon_odawara
o0h
PRO
0
280
【26新卒研修資料】TDD実装演習
dip_tech
PRO
0
120
第3木曜LT会 #28
tinykitten
PRO
0
120
属人化しないコード品質の作り方_2026.04.07.pdf
muraaano
0
270
TiDBのアーキテクチャから学ぶ分散システム入門 〜MySQL互換のNewSQLは何を解決するのか〜 / tidb-architecture-study
dznbk
1
200
PHPer、Cloudflare に引っ越す
suguruooki
1
110
アーキテクチャモダナイゼーションとは何か
nwiizo
19
5.5k
t *testing.T は どこからやってくるの?
otakakot
1
810
レガシーPHP転生 〜父がドメインエキスパートだったのでDDD+Claude Codeでチート開発します〜
panda_program
0
1.1k
Featured
See All Featured
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.6k
Writing Fast Ruby
sferik
630
63k
Designing for humans not robots
tammielis
254
26k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
160
Java REST API Framework Comparison - PWX 2021
mraible
34
9.3k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
140
Unsuck your backbone
ammeep
672
58k
Odyssey Design
rkendrick25
PRO
2
580
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
340
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
130
Transcript
DVR Networking tes-ng in Swi/.
Hi, I'm @soffes.
DVR is this thing I made when I worked at
Venmo.
Inspired by VCR for Ruby.
Records networking and plays it back in tests.
Using DVR
// Example XCTest func testTimeline() { let expectation = expectationWithDescription("Networking")
// Your network client let client = APIClient() client.getTimeline { success in XCTAssertTrue(success) expectation.fulfill() } waitForExpectationsWithTimeout(1, handler: nil) }
// DVR Session let dvr = Session(cassetteName: "timeline") // Pass
into initializer let client = APIClient(session: dir)
class APIClient { let session: NSURLSession init(session: NSURLSession = NSURLSession.sharedSession())
{ self.session = session } }
func getTimeline(completion: Bool -> Void) { let request = NSURLRequest(URL:
timelineURL) session.dataTaskWithRequest(request) { _, _, _ completion(true) }.resume() }
DVR Internals
class Session: NSURLSession { let cassetteName: String let backingSession: NSURLSession
init(cassetteName: StringbackingSession: NSURLSession = NSURLSession.sharedSession()) { self.cassetteName = cassetteName self.backingSession = backingSession super.init() } override func dataTaskWithRequest(request: NSURLRequest) -> NSURLSessionDataTask { return SessionDataTask(session: self, request: request) } }
Call resume() & then magic.
class SessionDataTask: NSURLSessionDataTask { override func resume() { // Playback
cassette on disk or record } }
Casse%es are stored as JSON.
{ "name" : "example", "interactions" : [ { "recorded_at" :
1434688721.440751, "request" : { "method" : "GET", "url" : "http:\/\/example.com" } "response" : { "body" : "hello", "status" : 200, "url" : "http:\/\/example.com\/", "headers" : { "Cache-Control" : "max-age=604800", "Content-Type" : "text\/plain", "Last-Modified" : "Fri, 09 Aug 2013 23:54:35 GMT", "Content-Length" : "5" } } } ] }
Why this over mocks?
Mocks are inherently fragile. You have to couple your tes7ng
code with the implementa7on details of your test. — Dave Abrahams, Protocol-Oriented Programming in Swi3
class DisabledSession: NSURLSession { override func dataTaskWithRequest(request: NSURLRequest, completionHandler: (NSData?,
NSURLResponse?, NSError?) -> Void) -> NSURLSessionDataTask? { XCTFail("Networking disabled") return nil } }
github.com/venmo/DVR
Thanks