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
0
6.5k
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
Tweet
Share
More Decks by Sam Soffes
See All by Sam Soffes
Starting a Mobile Design System
soffes
0
130
Building Functional Apps in Swift for iOS & OS X
soffes
5
13k
Go
soffes
2
610
Favorites
soffes
2
810
Seesaw — Under the Playground
soffes
2
410
Ubiquitously Connected to the World
soffes
0
220
API First, Then Apps
soffes
3
360
Building Realtime Apps
soffes
6
7.9k
Dispatch Awesome
soffes
8
940
Other Decks in Programming
See All in Programming
リリース時」テストから「デイリー実行」へ!開発マネージャが取り組んだ、レガシー自動テストのモダン化戦略
goataka
0
150
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
440
「コードは上から下へ読むのが一番」と思った時に、思い出してほしい話
panda728
PRO
39
26k
まだ間に合う!Claude Code元年をふりかえる
nogu66
5
920
Canon EOS R50 V と R5 Mark II 購入でみえてきた最近のデジイチ VR180 事情、そして VR180 静止画に活路を見出すまで
karad
0
140
Findy AI+の開発、運用におけるMCP活用事例
starfish719
0
1.9k
生成AI時代を勝ち抜くエンジニア組織マネジメント
coconala_engineer
0
36k
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
8
3.5k
これならできる!個人開発のすゝめ
tinykitten
PRO
0
140
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
420
SQL Server 2025 LT
odashinsuke
0
110
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
470
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
56
14k
The SEO Collaboration Effect
kristinabergwall1
0
320
Abbi's Birthday
coloredviolet
0
4k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
120
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
320
Color Theory Basics | Prateek | Gurzu
gurzu
0
160
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.8k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
The Cult of Friendly URLs
andyhume
79
6.7k
Navigating Team Friction
lara
191
16k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
130
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
0
97
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