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
160
Building Functional Apps in Swift for iOS & OS X
soffes
5
13k
Go
soffes
2
650
Favorites
soffes
2
830
Seesaw — Under the Playground
soffes
2
440
Ubiquitously Connected to the World
soffes
0
240
API First, Then Apps
soffes
3
390
Building Realtime Apps
soffes
6
8.1k
Dispatch Awesome
soffes
8
960
Other Decks in Programming
See All in Programming
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
3
1.1k
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
140
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
750
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
860
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.9k
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
630
Press start. Python's next generation.
willingc
PRO
3
310
Oxlintはいいぞ(続)
yug1224
1
550
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
0
440
高専キャリア LT 発表内容
crysta1221
6
5.6k
Seeing Through Serverless: Observability for AWS Lambda with ADOT and CloudWatch Application Signals
seike460
PRO
1
120
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
120
Featured
See All Featured
Joys of Absence: A Defence of Solitary Play
codingconduct
1
480
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
260
For a Future-Friendly Web
brad_frost
183
10k
Facilitating Awesome Meetings
lara
57
7.1k
We Have a Design System, Now What?
morganepeng
55
8.3k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Become a Pro
speakerdeck
PRO
31
6.2k
Google's AI Overviews - The New Search
badams
0
1.6k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
500
The Mindset for Success: Future Career Progression
greggifford
PRO
0
490
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