Class WithinTimeBudgetCapturingAssertion<T>
- Namespace
- TimeAssertions.TUnit
- Assembly
- TimeAssertions.TUnit.dll
Capturing variant of WithinTimeBudgetAssertion<T>. Same wall-clock budget
behaviour, but additionally invokes the supplied capture callback with the measured
elapsed duration regardless of whether the budget was met. Useful for tests that need to
log the observed timing or feed it into a follow-up HasAdvancedApproximately check.
[AssertionExtension("WithinTimeBudgetCapturing")]
public sealed class WithinTimeBudgetCapturingAssertion<T> : Assertion<T>, IAssertion
Type Parameters
TThe value type of the underlying assertion.
- Inheritance
-
Assertion<T>WithinTimeBudgetCapturingAssertion<T>
- Implements
-
IAssertion
- Inherited Members
-
Assertion<T>.AssertAsync()Assertion<T>.GetAwaiter()Assertion<T>.AndAssertion<T>.Or
Remarks
The capture callback runs even when the budget is exceeded, so a test can still surface the observed duration in its failure diagnostic before the budget-overrun assertion exception propagates.
External cancellation propagates intact AND skips the capture callback. When the wrapped operation's CancellationToken cancels, the OperationCanceledException is re-thrown via ExceptionDispatchInfo so the test is recorded as cancelled, not failed. The capture callback is NOT invoked on cancellation: a partial elapsed time from a cancelled operation would mislead consumers since the operation did not complete normally. Non-OCE source exceptions still invoke the capture (the elapsed duration is meaningful in that case).
Composes via .And like the non-capturing variant:
var elapsed = TimeSpan.Zero;
await Assert.That(asyncOp)
.IsEqualTo(42)
.And.WithinTimeBudgetCapturing(TimeSpan.FromMilliseconds(500), e => elapsed = e);
// 'elapsed' now holds the wall-clock duration of the asyncOp evaluator.
Constructors
WithinTimeBudgetCapturingAssertion(AssertionContext<T>, TimeSpan, Action<TimeSpan>)
Initialises the capturing assertion with a wall-clock budget and an elapsed callback. Called by the TUnit source generator.
public WithinTimeBudgetCapturingAssertion(AssertionContext<T> context, TimeSpan budget, Action<TimeSpan> capture)
Parameters
contextAssertionContext<T>The assertion context supplied by TUnit.
budgetTimeSpanThe maximum wall-clock duration the preceding assertion's evaluator is allowed to take.
captureAction<TimeSpan>Callback invoked with the measured elapsed duration. Called once per assertion evaluation, including when the budget is exceeded.
Exceptions
- ArgumentOutOfRangeException
budgetis negative.- ArgumentNullException
captureis null.
Methods
CheckAsync(EvaluationMetadata<T>)
Implements the specific check logic for this assertion. Called after the context has been evaluated. Override this method if your assertion uses the default AssertAsync() flow. If you override AssertAsync() with custom logic (like AndAssertion/OrAssertion), you don't need to implement this.
protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<T> metadata)
Parameters
metadataEvaluationMetadata<T>Metadata about the evaluation including value, exception, and timing information
Returns
- Task<AssertionResult>
The result of the assertion check
GetExpectation()
Gets a human-readable description of what this assertion expects. Used in error messages.
protected override string GetExpectation()