FakeApiClient
class FakeApiClient(handler: suspend (FakeApiClient.RecordedRequest) -> Any? = { Unit }) : NetworkDataSource
A scriptable NetworkDataSource (and therefore ApiClient) that never touches HTTP: every call is recorded and answered by handler. Use it to unit-test code that sits above the client — a GenericRepository, a MutationQueue, a ViewModel — without building a MockEngine or encoding JSON.
The value handler returns is handed back as the decoded response, so it must match the type the caller requests (return Unit for execute). Throw a io.github.maniramezan.kenwork.network.NetworkError to simulate a failure:
val api = FakeApiClient { request ->
when (request.index) {
0 -> throw NetworkError.Timeout
else -> Video(id = 42, title = "Hi")
}
}
val repository = GenericRepository<Video>(api, CacheBasedLocalDataSource(InMemoryCache()))Content copied to clipboard
Parameters
handler
produces the response for each RecordedRequest; defaults to returning Unit.
Types
Link copied to clipboard
data class RecordedRequest(val endpoint: NetworkEndpoint, val body: Any?, val responseType: TypeInfo, val index: Int)
One call made through this client.