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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
630
Favorites
soffes
2
820
Seesaw — Under the Playground
soffes
2
430
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
さぁV100、メモリをお食べ・・・
nilpe
0
130
AIチームを指揮するOSS「TAKT」活用術 / How to Use “TAKT,” an OSS Tool for Orchestrating AI Teams
nrslib
6
800
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
4
1.1k
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
470
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
240
These Five Tricks Can Make Your Apps Greener, Cheaper, & Nicer
hollycummins
0
270
inferと仲良くなる10分間
ryokatsuse
1
360
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
130
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
1.2k
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
3.3k
Lessons from Spec-Driven Development
simas
PRO
0
130
ふつうのFeature Flag実践入門
irof
7
3.5k
Featured
See All Featured
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
210
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
220
Building Adaptive Systems
keathley
44
3k
Abbi's Birthday
coloredviolet
2
7.9k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.3k
Scaling GitHub
holman
464
140k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
230
4 Signs Your Business is Dying
shpigford
187
22k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
A Soul's Torment
seathinner
6
2.9k
エンジニアに許された特別な時間の終わり
watany
107
250k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
590
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