Class WillLogAssertionExtensions
- Namespace
- TUnit.Assertions.Extensions
- Assembly
- LogAssertions.TUnit.dll
Generated extension methods for WillLogAssertion.
public static class WillLogAssertionExtensions
- Inheritance
-
WillLogAssertionExtensions
- Inherited Members
Methods
WillLog<TActual>(IAssertionSource<TActual>, TimeSpan, CancellationToken, string?, string?)
TUnit assertion that waits until a Microsoft.Extensions.Logging.Testing.FakeLogCollector holds matching log records: the event-driven
form of HasLogged() for records that background work logs after the call that started it returns. Entry
point: Assert.That(collector).WillLog(timeout, cancellationToken), then the same filters as
HasLogged() and a count terminator.
public static WillLogAssertion WillLog<TActual>(this IAssertionSource<TActual> source, TimeSpan timeout, CancellationToken cancellationToken = default, string? timeoutExpression = null, string? cancellationTokenExpression = null) where TActual : FakeLogCollector
Parameters
sourceIAssertionSource<TActual>timeoutTimeSpanThe longest the wait may take, in real time, counted from when the assertion runs. Must be positive and at most about 49 days. Choose it below the test's own timeout, so the wait fails with its diagnostics first.
cancellationTokenCancellationTokenEnds the wait early, for example the test's token.
timeoutExpressionstringcancellationTokenExpressionstring
Returns
Type Parameters
TActual
Remarks
It waits on the collector's stream of records, the one
WaitForLogAsync(FakeLogCollector, ILogRecordFilter, CancellationToken)
reads. The count is read from the collector right after the minimum number of matching records has been captured.
Records captured by then count, including ones logged before the assertion ran. A record that arrives after the
assertion has passed isn't checked: to pin "no more than one", assert HasLogged()...Once() after the work
has settled. The assertion fails when that count is above the maximum. When the timeout or the token ends
the wait, the records the collector holds at that moment decide: enough matching records pass (also a record
captured just before the timeout fired, or records already captured when the token was canceled before the
assertion ran); otherwise it fails with the expectation, the count, and the captured records, the same diagnostics
as HasLogged(), not a bare cancellation. A chain whose levels all sit below the collector's capture floor
fails at once, since such a record can never be captured.
Clock: the timeout runs in real time (System), whatever clock the collector uses,
and starts when the assertion runs (at await), not when the chain is built. It bounds how long the
test waits for real background work. When that work waits on a fake clock, start the assertion first, advance the
clock, then await the assertion: put the chain in a local function that awaits it (TUnit's analyzer requires every
Assert.That to be awaited), call it, advance, and await the task it returned. That
await is what surfaces the failure message: a helper that advances until the task completes
surfaces it only if the helper awaits the task too.
A wait proves that records arrived, not that no more follow, so there is no Never() or AtMost(n): to
pin "no more than one", let the work finish and assert HasLogged()...Once(); for absence, use
HasNotLogged() after the work has finished. The wait is built on
WaitForLogsAsync(FakeLogCollector, ILogRecordFilter, int, CancellationToken): no polling and no real-time delay; the timeout is a
bound, not a poll interval.
Exceptions
- ArgumentOutOfRangeException
timeoutis zero, negative (including InfiniteTimeSpan), or longer than a CancellationTokenSource accepts.