Skip to content

Instantly share code, notes, and snippets.

@thexande
Created December 20, 2018 05:03
Show Gist options
  • Save thexande/875296283fcc0d7977bfe42dd9b95e1d to your computer and use it in GitHub Desktop.
Save thexande/875296283fcc0d7977bfe42dd9b95e1d to your computer and use it in GitHub Desktop.
import Apollo
final class MockNetworkTransport: NetworkTransport {
enum NetworkError: Error {
case networkFailure
}
private final class MockTask: Cancellable {
func cancel() { }
}
let simulateNetworkFailure: Bool
let body: JSONObject
init(body: JSONObject, simulateNetworkFailure: Bool = false) {
self.body = body
self.simulateNetworkFailure = simulateNetworkFailure
}
func send(operation: Operation,
completionHandler: @escaping (_ response: GraphQLResponse?, _ error: Error?) -> Void) -> Cancellable {
DispatchQueue.global(qos: .default).async {
if self.simulateNetworkFailure {
completionHandler(nil, NetworkError.networkFailure)
return
}
completionHandler(GraphQLResponse(operation: operation,
body: self.body), nil)
}
return MockTask()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment