Mocks allow us to write fast tests that don't mess with production data. Without OCMock we will need to write our own mocks, but it doesn't have to be a lot of work. We will look at techniques for practical mocking in Swift that will allow us to create simple, easy to maintain mocks for the most important parts of our codebase.
Presented on March 4th, 2016 at try! Swift in Tokyo.
6. let session = NSURLSession()
let url = NSURL(string: "http://www.tryswiftconf.com")!
let task = session.dataTaskWithURL(url) { (data, _, _) -> Void in
if let data = data {
let string = String(data: data, encoding: NSUTF8StringEncoding)
print(string)
}
}
task.resume()
55. WhyIt’s BadTo MockTypesYou Don’tOwn
» Have to be sure that the behavior you implement in
a mock matches the external library
» The external library could change, breaking your
mock
59. Mocking NSNotificationCenterWas NotWorth It
» Complex !
» Injected throughout entire codebase "
» Limited functionality compared to real object #
» Solves a problem that doesn’t really exist $
66. WhatMakesaGood Mock
» Quick and easy to write
» Relatively short and does not contain tons of
information you don’t need
» Legitimate reason to not use real object