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
100
Building Functional Apps in Swift for iOS & OS X
soffes
5
13k
Go
soffes
2
580
Favorites
soffes
2
800
Seesaw — Under the Playground
soffes
2
400
Ubiquitously Connected to the World
soffes
0
200
API First, Then Apps
soffes
3
350
Building Realtime Apps
soffes
6
7.7k
Dispatch Awesome
soffes
8
920
Other Decks in Programming
See All in Programming
C#および.NETに対する誤解をひも解く
ymd65536
0
180
Progressive Web Apps for Rails developers
siaw23
2
520
NANIMACHI
naokiito
0
920
Frontend Magic mit CSS Houdini
joergneumann
0
420
API Platform for Laravel
dunglas
1
1.2k
Pythonによるイベントソーシングへの挑戦と現状に対する考察 / Challenging Event Sourcing with Python and Reflections on the Current State
nrslib
3
800
Unlocking Python's Core Magic
leew
0
100
5年分のツケを一気に払った話
soogie
3
950
Compose Multiplatform과 Ktor로 플랫폼의 경계를 넘어보자
kwakeuijin
0
200
pytest プラグインを開発して DRY に自動テストを書こう
inuatsu
2
230
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
4
93k
ECS向けのドリフト検知機構を実装してみた
tkikuc
0
260
Featured
See All Featured
Designing with Data
zakiwarfel
98
5.1k
Mobile First: as difficult as doing things right
swwweet
221
8.8k
We Have a Design System, Now What?
morganepeng
48
7.1k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
7
550
Java REST API Framework Comparison - PWX 2021
mraible
PRO
27
7.5k
GraphQLの誤解/rethinking-graphql
sonatard
65
9.9k
Building a Modern Day E-commerce SEO Strategy
aleyda
36
6.8k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
26
4k
Building Better People: How to give real-time feedback that sticks.
wjessup
359
19k
Build The Right Thing And Hit Your Dates
maggiecrowley
30
2.3k
Designing the Hi-DPI Web
ddemaree
279
34k
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