I am writing test for my project and declared a remote API client protolcol:
public protocol ApiClient {
func load(completion: @escaping ([Any]?, String?))
}
and defined a moc api client that confirms to ApiClient
:
class MocApiClient: ApiClient {
func loadFlights(completion: @escaping ([Any]?, String?)) {
// Load a sample JSON file and return it as response
}
}
this way I am able return a response by loading a JSON
file. This is the happy path of the test. After it I started to think about testing different possible response types and decided that I should be able to alter behaviour of the MocApiClient
and defined this:
enum TestPath {
case success
case failure
}
and using it with MocApiClient
:
class MocApiClient: ApiClient {
var path: TestPath = .success
func load(completion: @escaping ([Any]?, String?) -> Void) {
switch path {
case .success:
completion([...], nil)
case .failure:
completion(nil, "error message")
}
}
}
Doyu think this is a good solution? Do you have any beter approachs?
Aucun commentaire:
Enregistrer un commentaire