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
110
Building Functional Apps in Swift for iOS & OS X
soffes
5
13k
Go
soffes
2
600
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.8k
Dispatch Awesome
soffes
8
930
Other Decks in Programming
See All in Programming
CSC307 Lecture 17
javiergs
PRO
0
110
複数アプリケーションを育てていくための共通化戦略
irof
10
3.8k
Elixir で IoT 開発、 Nerves なら簡単にできる!?
pojiro
1
120
TypeScript LSP の今までとこれから
quramy
1
500
FormFlow - Build Stunning Multistep Forms
yceruto
1
160
事業戦略を理解してソフトウェアを設計する
masuda220
PRO
22
6k
関数型まつり2025登壇資料「関数プログラミングと再帰」
taisontsukada
2
800
業務自動化をJavaとSeleniumとAWS Lambdaで実現した方法
greenflagproject
1
100
F#で自在につくる静的ブログサイト - 関数型まつり2025
pizzacat83
0
290
Julia という言語について (FP in Julia « SIDE: F ») for 関数型まつり2025
antimon2
3
920
Rails産でないDBを Railsに引っ越すHACK - Omotesando.rb #110
lnit
1
160
try-catchを使わないエラーハンドリング!? PHPでResult型の考え方を取り入れてみよう
kajitack
3
500
Featured
See All Featured
Done Done
chrislema
184
16k
How STYLIGHT went responsive
nonsquared
100
5.6k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Building Adaptive Systems
keathley
43
2.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
43
2.4k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Embracing the Ebb and Flow
colly
86
4.7k
Scaling GitHub
holman
459
140k
Making Projects Easy
brettharned
116
6.2k
Making the Leap to Tech Lead
cromwellryan
134
9.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Raft: Consensus for Rubyists
vanstee
139
7k
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