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
6.6k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
150
Building Functional Apps in Swift for iOS & OS X
soffes
5
13k
Go
soffes
2
640
Favorites
soffes
2
830
Seesaw — Under the Playground
soffes
2
440
Ubiquitously Connected to the World
soffes
0
230
API First, Then Apps
soffes
3
380
Building Realtime Apps
soffes
6
8k
Dispatch Awesome
soffes
8
950
Other Decks in Programming
See All in Programming
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
170
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
120
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
430
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
570
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.5k
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
2.7k
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
120
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
340
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
140
信頼性について考えてみる(SRE NEXT 2026 miniLT)
hayama17
0
230
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
2
1.3k
SREの積み重ねがAI駆動開発のガードレールになった ― 7つの実践/SRE Guardrails The 7
tomoyakitaura
9
5.8k
Featured
See All Featured
Joys of Absence: A Defence of Solitary Play
codingconduct
1
420
Evolving SEO for Evolving Search Engines
ryanjones
0
250
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
450
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Making Projects Easy
brettharned
120
6.7k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Raft: Consensus for Rubyists
vanstee
141
7.6k
Facilitating Awesome Meetings
lara
57
7k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
330
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
390
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