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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Sam Soffes
October 08, 2015
Programming
6.5k
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
820
Seesaw — Under the Playground
soffes
2
430
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
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
230
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
110
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
390
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
3
1.3k
net-httpのHTTP/2対応について
naruse
0
470
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
530
The Arts and Crafts of Work in the AI Era — Toward Mastery in Software Development
kuranuki
1
750
Oxcを導入して開発体験が向上した話
yug1224
4
310
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
260
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
360
LLM Plugin for Node-REDの利用方法と開発について
404background
0
170
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
120
Featured
See All Featured
Paper Plane (Part 1)
katiecoart
PRO
0
8.8k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
270
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
130
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
160
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Exploring anti-patterns in Rails
aemeredith
3
400
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
140
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
390
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
ラッコキーワード サービス紹介資料
rakko
1
3.6M
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
250
We Are The Robots
honzajavorek
0
240
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