Class UnwrapExtensions
Provides Unwrap() extension methods for extracting values from Result<TValue>
and Maybe<T> in test code. These methods throw a descriptive exception on failure/none
rather than returning the raw .Value, which avoids TRLS003 warnings in test projects.
public static class UnwrapExtensions
- Inheritance
-
UnwrapExtensions
- Inherited Members
Remarks
Intended for test code only. Do not use in production code — use pattern matching,
Match, GetValueOrDefault, or other ROP operations instead.
Typical usage after a FluentAssertions guard:
result.Should().BeSuccess();
var value = result.Unwrap(); // Safe — we know it's a success
Methods
UnwrapAsync<T>(Task<Result<T>>)
Awaits the task and extracts the value from a successful result, or throws UnwrapFailedException with the error details if the result is a failure.
public static Task<T> UnwrapAsync<T>(this Task<Result<T>> resultTask)
Parameters
Returns
- Task<T>
The success value.
Type Parameters
TType of the result value.
Exceptions
- UnwrapFailedException
Thrown when the result is a failure.
UnwrapAsync<T>(ValueTask<Result<T>>)
Awaits the value task and extracts the value from a successful result, or throws UnwrapFailedException with the error details if the result is a failure.
public static ValueTask<T> UnwrapAsync<T>(this ValueTask<Result<T>> resultTask)
Parameters
Returns
- ValueTask<T>
The success value.
Type Parameters
TType of the result value.
Exceptions
- UnwrapFailedException
Thrown when the result is a failure.
Unwrap<T>(Maybe<T>)
Extracts the value from a Maybe that has a value, or throws UnwrapFailedException if the Maybe is None.
public static T Unwrap<T>(this Maybe<T> maybe) where T : notnull
Parameters
maybeMaybe<T>The Maybe to unwrap.
Returns
- T
The contained value.
Type Parameters
TType of the Maybe value.
Exceptions
- UnwrapFailedException
Thrown when the Maybe is None.
Unwrap<T>(Result<T>)
Extracts the value from a successful result, or throws UnwrapFailedException with the error details if the result is a failure.
public static T Unwrap<T>(this Result<T> result)
Parameters
resultResult<T>The result to unwrap.
Returns
- T
The success value.
Type Parameters
TType of the result value.
Exceptions
- UnwrapFailedException
Thrown when the result is a failure.