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
89
Building Functional Apps in Swift for iOS & OS X
soffes
5
13k
Go
soffes
2
580
Favorites
soffes
2
790
Seesaw — Under the Playground
soffes
2
390
Ubiquitously Connected to the World
soffes
0
190
API First, Then Apps
soffes
3
340
Building Realtime Apps
soffes
6
7.7k
Dispatch Awesome
soffes
8
910
Other Decks in Programming
See All in Programming
GitHub Copilotのススメ
marcy731
1
200
GitHub Actionsで泣かないためにやっておきたい設定 / Recommended GHA settings to avoid crying
pinkumohikan
3
540
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
210
Blue/Greenデプロイの導入による 運用フローの改善
kudoas
1
380
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
650
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
190
Milestoner
bkuhlmann
1
410
新宿ダンジョンを可視化してみた
satoshi7190
2
260
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
Ruby GitHub Packages
bkuhlmann
0
630
冗長なエラーログを削減し、スタックトレースを手に入れる / Reducing Verbose Error Logs and Obtaining Stack Traces
upamune
0
790
PHPはいつから死んでいるかの調査
chiroruxx
1
400
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
16
2.6k
Web development in the modern age
philhawksworth
202
10k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
6.9k
[RailsConf 2023] Rails as a piece of cake
palkan
23
4k
Being A Developer After 40
akosma
57
580k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
244
20k
The Cost Of JavaScript in 2023
addyosmani
16
3.9k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
274
13k
Fireside Chat
paigeccino
21
2.6k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
116
18k
Teambox: Starting and Learning
jrom
128
8.4k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
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