Tasks
Describes the possible expectations for a Task or ValueTask that is passed to Expect.That directly.
| Expectation | Negated | Summary |
|---|---|---|
Throws, … | DoesNotThrow | the running Task or ValueTask faults, as for a delegate |
ExecutesIn | the rest of the running task completes within the expected time | |
expectations on T | the result of a Task<T> or ValueTask<T> meets them | |
That<Task>(…) | expectations on the task object itself |
Running tasks
A Task or ValueTask is treated like a delegate: an asynchronous operation without return value
that is already running. All expectations for a delegate without return value are available:
Task ImportAlbumAsync(string title) => Task.CompletedTask;
await That(ImportAlbumAsync("Abbey Road")).DoesNotThrow();
await That(Task.FromException(new CustomException("Yesterday"))).Throws<CustomException>();
The task is already running, so ExecutesIn only measures the remaining time and a timeout only stops waiting for it,
see outcome. Pass a delegate
(That(() => ImportAlbumAsync("Abbey Road"))) to measure or cancel the whole execution.
A ValueTask is consumed by Expect.That, so it must not be awaited anywhere else.
Tasks with a result
A Task<T> or ValueTask<T> behaves differently: it is awaited and its result becomes the subject, so all
expectations for T are available. To check the exception or the execution time of such a task, wrap it in a lambda
(() => task), which turns it into a delegate:
Task<int> playCount = Task.FromResult(42);
await That(playCount).IsEqualTo(42);
await That(() => playCount).DoesNotThrow();
The task object
An expectation for an object, such as IsNotNull(), does not check what a task passed to That(task) does,
so the analyzer rule aweXpect0004 reports it as an error. To make an expectation
about the task object rather than about what it does, state the type explicitly:
await That<Task>(task).IsNotNull();
Faulted tasks
A task, like an asynchronous delegate, is awaited, so when it faults with several exceptions
(e.g. from Task.WhenAll), only the first one counts as thrown, just as with await. A failure message lists the
others under "Other exceptions".