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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
3
840
ビジネスモデルから紐解く、AI+型駆動開発
hirokiomote
2
5.2k
Spec-Driven Development with AI-Agents: From High-Level Requirements to Working Software
antonarhipov
2
440
New "Type" system on PicoRuby
pocke
1
430
今さら聞けないCancellationToken
htkym
0
220
Lessons from Spec-Driven Development
simas
PRO
0
130
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
110
タクシーアプリ『GO』の バックエンド開発のおける AI利活用と若者のすべて
pyama86
3
1.8k
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
1.8k
ふつうのFeature Flag実践入門
irof
7
3.5k
oxlintはeslint/typescript-eslintを置き換えられるのか
shomafujita
2
310
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
2.3k
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
28
2.6k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
From π to Pie charts
rasagy
0
200
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
380
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
How GitHub (no longer) Works
holman
316
150k
The Curious Case for Waylosing
cassininazir
1
370
Designing Experiences People Love
moore
143
24k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
Mind Mapping
helmedeiros
PRO
1
230
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