O'Reilly logo

Mastering iOS 11 Programming - Second Edition by Donny Wals

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mocking API responses

It's common practice to mock API responses when you're testing. In this segment, we're going to implement the mock API that was described before in order to improve the quality and reliability of our test. First, let's define our protocol. Create a new file in the app target and name it TriviaAPIProviding:

typealias QuestionsLoadedCallback = (JSON) -> Void

protocol TriviaAPIProviding {
    func loadTriviaQuestions(callback: @escaping QuestionsLoadedCallback)
}

The protocol only requires a single method right now. If you want to expand this app later, everything related to the Trivia API must be added to the protocol in order to make sure that you can create both an online version for your app and an offline version for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required