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
Prolog入門
qnighy
4
1k
『ドメイン駆動設計をはじめよう』中核の業務領域
masuda220
PRO
5
1k
Go1.23で入った errorsパッケージの小さなアプデ
kuro_kurorrr
2
400
Rubyのobject_id
qnighy
6
1.3k
Modern Angular with the NGRX Signal Store New Rules for Your Architecture @BASTA! 2024 in Mainz
manfredsteyer
PRO
0
160
Amazon Neptuneで始める初めてのグラフDB ー グラフDBを使う意味を考える ー
satoshi256kbyte
2
260
Debugging: All you need to know (for simultaneous interpreting)
jmatsu
2
930
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
520
The Shape of a Service Object
inem
0
530
Pythonで改めて考える「クラス(class)」の使いどころ
os1ma
5
1.1k
Hono・Prisma・AWSでGeoなAPI開発
nokonoko1203
5
680
Patched fetch did not work
quramy
4
410
Featured
See All Featured
Designing for Performance
lara
604
68k
Unsuck your backbone
ammeep
667
57k
RailsConf 2023
tenderlove
28
810
Building a Modern Day E-commerce SEO Strategy
aleyda
36
6.8k
Designing for humans not robots
tammielis
248
25k
Making the Leap to Tech Lead
cromwellryan
128
8.8k
Debugging Ruby Performance
tmm1
72
12k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.4k
BBQ
matthewcrist
83
9.2k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
0
130
The Invisible Side of Design
smashingmag
296
50k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.4k
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