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()))

Parameters

handler

produces the response for each RecordedRequest; defaults to returning Unit.

Constructors

Link copied to clipboard
constructor(handler: suspend (FakeApiClient.RecordedRequest) -> Any? = { 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.

Properties

Link copied to clipboard

A snapshot of every request made so far, in call order.

Functions

Link copied to clipboard
open suspend override fun <T> request(endpoint: NetworkEndpoint, body: Any?, bodyType: TypeInfo?, responseType: TypeInfo): T