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
実用!Hono RPC2026
yodaka
2
280
【26新卒研修資料】TDD実装演習
dip_tech
PRO
0
120
〜バイブコーディングを超えて〜 チームで実験し続けたAI駆動開発
tigertora7571
0
170
Programming with a DJ Controller — not vibe coding
m_seki
3
610
「話せることがない」を乗り越える 〜日常業務から登壇テーマをつくる思考法〜
shoheimitani
4
900
Swift Concurrency Type System
inamiy
1
560
検索設計から 推論設計への重心移動と Recall-First Retrieval
po3rin
4
1.3k
JAWS-UG横浜 #100 祝・第100回スペシャルAWS は VPC レスの時代へ
maroon1st
0
190
YJITとZJITにはイカなる違いがあるのか?
nakiym
0
260
Making the RBS Parser Faster
soutaro
0
590
Spec Driven Development | AI Summit Vilnius
danielsogl
PRO
1
120
クラウドネイティブなエンジニアに向ける Raycastの魅力と実際の活用事例
nealle
2
230
Featured
See All Featured
Building Adaptive Systems
keathley
44
3k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
Why Our Code Smells
bkeepers
PRO
340
58k
Documentation Writing (for coders)
carmenintech
77
5.3k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
70
39k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.1k
The Language of Interfaces
destraynor
162
26k
Paper Plane
katiecoart
PRO
1
49k
Done Done
chrislema
186
16k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
200
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