Mocking API responses

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

typealias QuestionsFetchedCallback = (JSON) -> Void

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

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

Get Mastering iOS 12 Programming - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.