Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing Testable Code

Bas Broek
November 28, 2019

Writing Testable Code

Workshop at The Swift Alps 2019

Bas Broek

November 28, 2019
Tweet

More Decks by Bas Broek

Other Decks in Programming

Transcript

  1. class Network { let urlSession: URLSession init(urlSession: URLSession = .shared)

    { self.urlSession = urlSession } } 6 — @basthomas
  2. protocol UIApplicationProtocol { func canOpenURL(_ url: URL) -> Bool func

    open( _ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any], completionHandler completion: ((Bool) -> Void)? ) } extension UIApplication: UIApplicationProtocol {} 10 — @basthomas
  3. class URLSessionDataTaskMock: URLSessionDataTask { private let closure: () -> Void

    init(closure: @escaping () -> Void) { self.closure = closure } override func resume() { closure() } } 12 — @basthomas
  4. class URLSessionMock: URLSession { var data: Data? var error: Error?

    override func dataTask( with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void ) -> URLSessionDataTask { let data = self.data let error = self.error return URLSessionDataTaskMock { completionHandler(data, nil, error) } } } 13 — @basthomas
  5. func assertManyThings( cookie: Cookie, file: StaticString = #file, line: UInt

    = #line ) { XCTAssertTrue(cookie.isDoughy, file: file, line: line) XCTAssertLessThan(cookie.calories, 200, file: file, line: line) } 15 — @basthomas
  6. "Your" turn: let's take a look at something you want

    to have covered with tests 24 — @basthomas