fork

fun <S : Any> ExpectStage<S>.fork(displayName: String, context: AggregateDslContext<S>, verifyError: Boolean, block: ForkedVerifiedStageDsl<S>.() -> Unit): DynamicNode

Creates a forked test scenario from the current ExpectStage.

This extension function enables branching of test execution from a verified aggregate state, allowing testing of alternative command sequences or error conditions. The fork creates an isolated test context that starts from the verified state of the current stage.

When verifyError is true, the fork expects an error to have occurred in the parent stage. When verifyError is false, the fork expects successful execution in the parent stage.

Return

a DynamicNode representing the forked test container, or a failed DynamicTest if an exception occurs

Parameters

S

the state type of the aggregate being tested

displayName

the name to display for this forked test branch in test reports

context

the DSL context for managing shared state across test stages

verifyError

whether to verify that an error occurred before forking (true) or successful execution (false)

block

the test scenario to execute in the forked context using ForkedVerifiedStageDsl

Throws

if an error occurs during fork setup or execution (wrapped in DynamicTest for reporting)

Example usage:

expectStage.fork("Test Error Handling", context, verifyError = true) {
whenCommand(ErrorCommand()) {
expectErrorType(IllegalArgumentException::class)
}
}