Testing with TUnit
Haitch.Unions.TUnit.Assertions provides assertions for Result<T> and Optional<T>, including their Task<...> forms.
Installation
Section titled “Installation”dotnet add package Haitch.Unions.TUnit.AssertionsResult assertions
Section titled “Result assertions”await result.AssertOk();await result.AssertOk(expectedValue);
await result.AssertError();await result.AssertError(expectedError);Per-kind helpers
Section titled “Per-kind helpers”Each of the six error kinds has a helper that asserts the result is Error and returns the typed error for further assertions:
ValidationError error = await result.AssertValidation();await Assert.That(error.Failures).ContainsKey("email");| Helper | Returns |
|---|---|
AssertNotFound() | NotFoundError |
AssertValidation() | ValidationError |
AssertConflict() | ConflictError |
AssertUnauthorized() | UnauthorizedError |
AssertForbidden() | ForbiddenError |
AssertUnexpected() | UnexpectedError |
Custom error kinds
Section titled “Custom error kinds”For a custom error kind, or any kind without a dedicated helper, use the general AssertError<TError>(). It’s declared alongside T in the same extension block as the Result<T> it applies to, so both type arguments are given explicitly at the call site:
var error = await result.AssertError<int, ValidationError>();Optional assertions
Section titled “Optional assertions”await optional.AssertSome();await optional.AssertSome(expectedValue);
await optional.AssertNone();Task forms
Section titled “Task forms”Every assertion above also works directly on Task<Result<T>> and Task<Optional<T>>, awaiting the task first:
await GetUserAsync(id).AssertOk();
var error = await GetUserAsync(id).AssertNotFound();