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
120
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
410
Ubiquitously Connected to the World
soffes
0
210
API First, Then Apps
soffes
3
360
Building Realtime Apps
soffes
6
7.8k
Dispatch Awesome
soffes
8
930
Other Decks in Programming
See All in Programming
XP, Testing and ninja testing ZOZ5
m_seki
2
270
デミカツ切り抜きで面倒くさいことはPythonにやらせよう
aokswork3
0
170
フロントエンド開発に役立つクライアントプログラム共通のノウハウ / Universal client-side programming best practices for frontend development
nrslib
7
3.9k
Your Perfect Project Setup for Angular @BASTA! 2025 in Mainz
manfredsteyer
PRO
0
120
Conquering Massive Traffic Spikes in Ruby Applications with Pitchfork
riseshia
0
150
非同期jobをtransaction内で 呼ぶなよ!絶対に呼ぶなよ!
alstrocrack
0
500
私達はmodernize packageに夢を見るか feat. go/analysis, go/ast / Go Conference 2025
kaorumuta
2
470
Swift Concurrency - 状態監視の罠
objectiveaudio
2
450
AIで開発生産性を上げる個人とチームの取り組み
taniigo
0
130
あなたの知らない「動画広告」の世界 - iOSDC Japan 2025
ukitaka
0
370
そのpreloadは必要?見過ごされたpreloadが技術的負債として爆発した日
mugitti9
2
2.9k
どの様にAIエージェントと 協業すべきだったのか?
takefumiyoshii
2
590
Featured
See All Featured
Speed Design
sergeychernyshev
32
1.1k
4 Signs Your Business is Dying
shpigford
185
22k
The Power of CSS Pseudo Elements
geoffreycrofte
79
6k
Making Projects Easy
brettharned
119
6.4k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
114
20k
Learning to Love Humans: Emotional Interface Design
aarron
274
40k
Site-Speed That Sticks
csswizardry
11
880
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
358
30k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Code Reviewing Like a Champion
maltzj
525
40k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.1k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
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