NetworkServiceMock
public final class NetworkServiceMock : NetworkService
Mocks a NetworkService.
You can configure expected results or errors to have a fully functional mock.
Example:
//Given
let networkServiceMock = NetworkServiceMock()
let resource: Resource<String> = //
//When
networkService.request(
resource,
onCompletion: { string in /*...
-
Count of all started requests
Declaration
Swift
public var requestCount: Int { get } -
Last executed request
Declaration
Swift
public var lastRequest: URLRequest? { get } -
All executed requests.
Declaration
Swift
public private(set) var lastRequests: [URLRequest] -
Set this to hava a custom networktask returned by the mock
Declaration
Swift
public var nextNetworkTask: NetworkTask? -
Creates an instace of
NetworkServiceMockDeclaration
Swift
public init() -
Fetches a resource asynchronously from remote location. Execution of the requests starts immediately. Execution happens on no specific queue. It dependes on the network access which queue is used. Once execution is finished either the completion block or the error block gets called. You decide on which queue these blocks get executed.
Example:
let networkService: NetworkService = // let resource: Resource<String> = // networkService.request(queue: .main, resource: resource, onCompletionWithResponse: { htmlText, response in print(htmlText, response) }, onError: { error in // Handle errors })Declaration
Swift
@discardableResult public func request<Result>(queue: DispatchQueue, resource: Resource<Result>, onCompletionWithResponse: @escaping (Result, HTTPURLResponse) -> Void, onError: @escaping (NetworkError) -> Void) -> NetworkTaskParameters
queueThe
DispatchQueueto execute the completion and error block on.resourceThe resource you want to fetch.
onCompletionWithResponseCallback which gets called when fetching and transforming into model succeeds.
onErrorCallback which gets called when fetching or transforming fails.
Return Value
a running network task
-
Will return an error to the current waiting request.
Declaration
Swift
public func returnError(with error: NetworkError)Parameters
errorthe error which gets passed to the caller
-
Will return a successful request, by using the given type
Tas serialized result of a request.Warning: This will crash if type
Tdoes not match your expected ResponseType of your current requestDeclaration
Swift
public func returnSuccess<T>(with serializedResponse: T, httpResponse: HTTPURLResponse = HTTPURLResponse())Parameters
datathe mock response from the server.
Data()by defaulthttpResponsethe mock
HTTPURLResponsefrom the server.HTTPURLResponse()by default
View on GitHub
NetworkServiceMock Class Reference