December 2018
Intermediate to advanced
414 pages
10h 19m
English
A common problem every developer encounters when adding tests to their software is how to test asynchronous code. Why is it a problem? It's because the flow of a test is expecting synchronous code. Let's consider, for example, testing a file loader that loads the content of a JSON file in an asynchronous way:
class FileLoader { func loadContent(fromFilename: String, onSuccess: @escaping (Any) -> Void) { guard let path = Bundle.main.path(forResource: fromFilename, ofType: "json") else { return } DispatchQueue.global(qos: .background).async { guard let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped) else { print("Error: Couldn't decode data") return } guard let decodedJSON = try? JSONSerialization.jsonObject( ...Read now
Unlock full access