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
590
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
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
350
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
450
testcontainers のススメ
sgash708
1
120
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
730
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
620
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
170
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
CSC509 Lecture 14
javiergs
PRO
0
130
Go の GC の不得意な部分を克服したい
taiyow
2
760
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
169
14k
Documentation Writing (for coders)
carmenintech
66
4.5k
How to Ace a Technical Interview
jacobian
276
23k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
160
Adopting Sorbet at Scale
ufuk
73
9.1k
Practical Orchestrator
shlominoach
186
10k
Mobile First: as difficult as doing things right
swwweet
222
9k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Unsuck your backbone
ammeep
669
57k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
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