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
Sam Soffes
October 08, 2015
Programming
6.6k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
150
Building Functional Apps in Swift for iOS & OS X
soffes
5
13k
Go
soffes
2
640
Favorites
soffes
2
830
Seesaw — Under the Playground
soffes
2
440
Ubiquitously Connected to the World
soffes
0
230
API First, Then Apps
soffes
3
380
Building Realtime Apps
soffes
6
8k
Dispatch Awesome
soffes
8
950
Other Decks in Programming
See All in Programming
吝嗇家のためのAI活用 / AI development for miser - ChatGPT + Issue Driven Development
tooppoo
0
170
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
290
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
130
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
210
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
180
ローカルLLMでどこまでコードが書けるか -縮小版 / How much code can be written on a local LLM Shortened
kishida
2
170
20260623_Loop Engineeringで自分の分身の問い合わせBotを作る
ryugen04
0
110
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
140
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
230
LaravelLive Japan の裏方のすべて — 第188回 PHP勉強会@東京 (2026-06-24)
suguruooki
2
150
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
260
AIで効率化できた業務・日常
ochtum
0
160
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.4k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
180
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
First, design no harm
axbom
PRO
2
1.2k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
220
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.1k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
880
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
410
KATA
mclloyd
PRO
35
15k
Side Projects
sachag
455
43k
Test your architecture with Archunit
thirion
1
2.3k
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