Class Expect
Fluent API for defining expected outcomes in Operation.Apply methods. Provides a more readable way to construct ExpectedOutcome and ExpectedOutcomes.
public static class Expect
- Inheritance
-
Expect
- Inherited Members
Examples
// Simple case - predicate validation, state unchanged return Expect.That<int>(r => r == state.Value, "should equal current value");
// With state transition return Expect.That<int>(r => r == state.Value + 1, "should increment") .ThenState(state with { Value = state.Value + 1 });
// Response-dependent state (requires mock for exploration) return Expect.That<PutResponse>(r => r.ETag != null, "should have ETag") .ThenState( (resp, s) => s.WithETag(((PutResponse)resp).ETag), mock: () => new PutResponse { ETag = Guid.NewGuid().ToString() });
// With background activity return Expect.That<CopyResponse>(r => r.StatusCode == 202, "accepted") .ThenState(state.WithPendingCopy()) .Triggers((resp, s) => new CopyCompletionStepFunction(...));
// Multiple possible outcomes return Expect.OneOf( Expect.That<int>(r => r > 0, "positive").ThenState(positiveState), Expect.That<int>(r => r == 0, "zero").ThenState(zeroState));
Methods
OneOf(params ExpectedOutcome[])
Creates multiple expected outcomes (non-deterministic behavior). Use when an operation can have multiple valid outcomes.
public static ExpectedOutcomes OneOf(params ExpectedOutcome[] outcomes)
Parameters
outcomesExpectedOutcome[]The possible outcomes.
Returns
- ExpectedOutcomes
An ExpectedOutcomes containing all possibilities.
OneOf<TResponse>(params ExpectedOutcomeBuilder<TResponse>[])
Creates multiple expected outcomes from builders (non-deterministic behavior). Use when an operation can have multiple valid outcomes.
public static ExpectedOutcomes OneOf<TResponse>(params ExpectedOutcomeBuilder<TResponse>[] builders)
Parameters
buildersExpectedOutcomeBuilder<TResponse>[]The outcome builders.
Returns
- ExpectedOutcomes
An ExpectedOutcomes containing all possibilities.
Type Parameters
TResponse
That(ResponseValidator)
Creates an expected outcome with a ResponseValidator.
public static ExpectedOutcomeBuilder<object> That(ResponseValidator validator)
Parameters
validatorResponseValidatorA ResponseValidator that validates responses.
Returns
- ExpectedOutcomeBuilder<object>
An ExpectedOutcomeBuilder<TResponse> for further configuration.
That<TResponse>(ResponseValidator)
Creates a typed expected outcome with a ResponseValidator.
public static ExpectedOutcomeBuilder<TResponse> That<TResponse>(ResponseValidator validator)
Parameters
validatorResponseValidatorA ResponseValidator that validates responses.
Returns
- ExpectedOutcomeBuilder<TResponse>
An ExpectedOutcomeBuilder<TResponse> for further configuration.
Type Parameters
TResponseThe type of response to validate.
That<TResponse>(Func<TResponse, ValidationResult>)
Creates an expected outcome with a ValidationResult-returning validator. This overload allows including the actual response value in error messages. Works well with FluentAssertions or FluentValidation libraries.
public static ExpectedOutcomeBuilder<TResponse> That<TResponse>(Func<TResponse, ValidationResult> validator)
Parameters
validatorFunc<TResponse, ValidationResult>A function that validates the response and returns a ValidationResult containing both the validity and an explanation message.
Returns
- ExpectedOutcomeBuilder<TResponse>
An ExpectedOutcomeBuilder<TResponse> for further configuration.
Type Parameters
TResponseThe type of response to validate.
Examples
// Rich error message with actual value Expect.That<int>(r => r > 0 ? ValidationResult.Valid() : ValidationResult.Invalid($"Expected positive but got {r}"))
That<TResponse>(Func<TResponse, bool>, string)
Creates an expected outcome with a predicate-based response validator.
public static ExpectedOutcomeBuilder<TResponse> That<TResponse>(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
- ExpectedOutcomeBuilder<TResponse>
An ExpectedOutcomeBuilder<TResponse> for further configuration.
Type Parameters
TResponseThe type of response to validate.
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 static ExpectedOutcomeBuilder<Exception> 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
- ExpectedOutcomeBuilder<Exception>
An ExpectedOutcomeBuilder<TResponse> for further configuration.
Type Parameters
TExceptionThe expected exception type.
Examples
// Expect operation to throw with specific message return Expect.Throws<InsufficientFundsException>( ex => ex.Message.Contains("insufficient"), "should throw with 'insufficient' message") .SameState();
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 static ExpectedOutcomeBuilder<Exception> Throws<TException>(string explanation = null) where TException : Exception
Parameters
explanationstringOptional explanation for error messages.
Returns
- ExpectedOutcomeBuilder<Exception>
An ExpectedOutcomeBuilder<TResponse> for further configuration.
Type Parameters
TExceptionThe expected exception type.
Examples
// Expect operation to throw InsufficientFundsException return Expect.Throws<InsufficientFundsException>() .SameState();
Unit(string)
Creates an expected outcome for a void-returning operation. Use this when the operation returns Unit(string) (i.e., performs an action but returns no meaningful value).
public static ExpectedOutcomeBuilder<Unit> Unit(string explanation = null)
Parameters
explanationstringOptional explanation for error messages.
Returns
- ExpectedOutcomeBuilder<Unit>
An ExpectedOutcomeBuilder<TResponse> for further configuration.
Examples
// Expect Push operation to succeed (returns Unit) return Expect.Unit() .ThenState(updatedState);