Class ExpectContext<TResponse, TState>
Provides type-inferred expect methods for use within an Operation. This class knows both the response type and state type, allowing methods like That(Func<TResponse, bool>, string) and ThenState(Action<TState>) to work without explicit generic type parameters.
public class ExpectContext<TResponse, TState> where TState : class, IState
Type Parameters
TResponseThe response type of the operation.
TStateThe state type of the operation.
- Inheritance
-
ExpectContext<TResponse, TState>
- Inherited Members
Constructors
ExpectContext()
Creates a new ExpectContext instance.
public ExpectContext()
Methods
OneOf(params ExpectedOutcome[])
Creates multiple expected outcomes (non-deterministic behavior). Use when an operation can have multiple valid outcomes.
public ExpectedOutcomes OneOf(params ExpectedOutcome[] outcomes)
Parameters
outcomesExpectedOutcome[]The possible outcomes.
Returns
- ExpectedOutcomes
An ExpectedOutcomes containing all possibilities.
OneOf(params TypedExpectBuilder<TResponse, TState>[])
Creates multiple expected outcomes from typed builders (non-deterministic behavior). Use when an operation can have multiple valid outcomes.
public ExpectedOutcomes OneOf(params TypedExpectBuilder<TResponse, TState>[] builders)
Parameters
buildersTypedExpectBuilder<TResponse, TState>[]The outcome builders.
Returns
- ExpectedOutcomes
An ExpectedOutcomes containing all possibilities.
That(ResponseValidator)
Creates an expected outcome with a ResponseValidator.
public TypedExpectBuilder<TResponse, TState> That(ResponseValidator validator)
Parameters
validatorResponseValidatorA ResponseValidator that validates responses.
Returns
- TypedExpectBuilder<TResponse, TState>
A TypedExpectBuilder<TResponse, TState> for further configuration.
That(Func<TResponse, ValidationResult>)
Creates an expected outcome with a ValidationResult-returning validator. This overload allows including the actual response value in error messages.
public TypedExpectBuilder<TResponse, TState> That(Func<TResponse, ValidationResult> validator)
Parameters
validatorFunc<TResponse, ValidationResult>A function that validates the response and returns a ValidationResult.
Returns
- TypedExpectBuilder<TResponse, TState>
A TypedExpectBuilder<TResponse, TState> for further configuration.
That(Func<TResponse, bool>, string)
Creates an expected outcome with a predicate-based response validator. The response type is inferred from the operation context.
public TypedExpectBuilder<TResponse, TState> That(Func<TResponse, bool> predicate, string explanation = null)
Parameters
predicateFunc<TResponse, bool>A predicate that returns true if the response is valid.
explanationstringA description of what the predicate checks (used in error messages).
Returns
- TypedExpectBuilder<TResponse, TState>
A TypedExpectBuilder<TResponse, TState> for further configuration.
Examples
return Expect.That(r => r > 0, "should be positive") .ThenState(nextState => nextState.Count++);
Throws<TException>(Func<TException, bool>, string)
Creates an expected outcome for an operation that should throw an exception, with additional validation on the exception.
public TypedExpectBuilder<Exception, TState> Throws<TException>(Func<TException, bool> predicate, string explanation = null) where TException : Exception
Parameters
predicateFunc<TException, bool>Predicate to validate the exception.
explanationstringOptional explanation for error messages.
Returns
- TypedExpectBuilder<Exception, TState>
A TypedExpectBuilder<TResponse, TState> for further configuration.
Type Parameters
TExceptionThe expected exception type.
Throws<TException>(string)
Creates an expected outcome for an operation that should throw an exception. Use this when the operation is expected to throw rather than return normally.
public TypedExpectBuilder<Exception, TState> Throws<TException>(string explanation = null) where TException : Exception
Parameters
explanationstringOptional explanation for error messages.
Returns
- TypedExpectBuilder<Exception, TState>
A TypedExpectBuilder<TResponse, TState> for further configuration.
Type Parameters
TExceptionThe expected exception type.
Examples
return Expect.Throws<InvalidOperationException>() .SameState();
Unit(string)
Creates an expected outcome for a void-returning operation. Use this when the operation returns Unit (i.e., performs an action but returns no meaningful value).
public TypedExpectBuilder<Unit, TState> Unit(string explanation = null)
Parameters
explanationstringOptional explanation for error messages.
Returns
- TypedExpectBuilder<Unit, TState>
A TypedExpectBuilder<TResponse, TState> for further configuration.
Examples
return Expect.Unit() .ThenState(nextState => nextState.Items.Add(item));