ScriptedResponder

class ScriptedResponder<Request, Response>(outcomes: List<Result<Response>>, fallback: Result<Response>? = null)(source)

A scripted fake that records each request and answers it with the next scripted outcome.

Forward each call of an injected dependency to respond. Calls are numbered from zero in arrival order, and call n always receives outcome n, even when an earlier call is held and finishes later. hold returns a gate that suspends one call after it is recorded and before it is answered, so a test can observe in-flight state deterministically.

val responder = ScriptedResponder<String, Int>(listOf(Result.failure(TestError()), Result.success(42)))
val fetch: suspend (String) -> Int = responder::respond

Parameters

outcomes

Successes and failures, indexed by call number.

fallback

The outcome for calls after the script is used; null throws Exhausted.

Constructors

Link copied to clipboard
constructor(outcomes: List<Result<Response>>, fallback: Result<Response>? = null)

Types

Link copied to clipboard
class Exhausted(val callIndex: Int) : IllegalStateException

Thrown when a call arrives after every scripted outcome is used and no fallback exists.

Properties

Link copied to clipboard

The number of calls received.

Link copied to clipboard

The recorded requests in arrival order, including calls still held or in flight.

Functions

Link copied to clipboard
fun hold(call: Int): AsyncGate

Returns a gate that holds the call with zero-based index call until opened.

Link copied to clipboard
suspend fun respond(request: Request): Response

Records request and answers it with the next scripted outcome.

Link copied to clipboard
suspend fun waitForCalls(count: Int, timeout: Duration = 2.seconds): List<Request>

Waits in real time until at least count calls have been recorded.