Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
Writing Testable Code 1 — @basthomas
Slide 2
Slide 2 text
Tests, what are they good for? 2 — @basthomas
Slide 3
Slide 3 text
What do you want to verify? 3 — @basthomas
Slide 4
Slide 4 text
How do you want to verify it? 4 — @basthomas
Slide 5
Slide 5 text
Dependency injection: three ways 5 — @basthomas
Slide 6
Slide 6 text
class Network { let urlSession: URLSession init(urlSession: URLSession = .shared) { self.urlSession = urlSession } } 6 — @basthomas
Slide 7
Slide 7 text
class Network { var urlSession = URLSession.shared } 7 — @basthomas
Slide 8
Slide 8 text
class Network { func request(using urlSession: URLSession = .shared) { } } 8 — @basthomas
Slide 9
Slide 9 text
Mocking through Protocols 9 — @basthomas
Slide 10
Slide 10 text
protocol UIApplicationProtocol { func canOpenURL(_ url: URL) -> Bool func open( _ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any], completionHandler completion: ((Bool) -> Void)? ) } extension UIApplication: UIApplicationProtocol {} 10 — @basthomas
Slide 11
Slide 11 text
Mocking through Subclassing 11 — @basthomas
Slide 12
Slide 12 text
class URLSessionDataTaskMock: URLSessionDataTask { private let closure: () -> Void init(closure: @escaping () -> Void) { self.closure = closure } override func resume() { closure() } } 12 — @basthomas
Slide 13
Slide 13 text
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
Slide 14
Slide 14 text
Custom asserts 14 — @basthomas
Slide 15
Slide 15 text
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
Slide 16
Slide 16 text
XCTest 16 — @basthomas
Slide 17
Slide 17 text
XCTUnwrap XCTNoThrow 17 — @basthomas
Slide 18
Slide 18 text
Let's talk about "flaky" 18 — @basthomas
Slide 19
Slide 19 text
19 — @basthomas
Slide 20
Slide 20 text
Make sure your architecture takes testability into account 20 — @basthomas
Slide 21
Slide 21 text
Tests, Code, Documentation 21 — @basthomas
Slide 22
Slide 22 text
Treat your test code like your real code 22 — @basthomas
Slide 23
Slide 23 text
Reviews of tests, not reviews with tests 23 — @basthomas
Slide 24
Slide 24 text
"Your" turn: let's take a look at something you want to have covered with tests 24 — @basthomas