CSharpEssentials.Results.Testing 9.0.0

dotnet add package CSharpEssentials.Results.Testing --version 9.0.0
                    
NuGet\Install-Package CSharpEssentials.Results.Testing -Version 9.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSharpEssentials.Results.Testing" Version="9.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.Results.Testing" Version="9.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.Results.Testing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSharpEssentials.Results.Testing --version 9.0.0
                    
#r "nuget: CSharpEssentials.Results.Testing, 9.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CSharpEssentials.Results.Testing@9.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSharpEssentials.Results.Testing&version=9.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.Results.Testing&version=9.0.0
                    
Install as a Cake Tool

CSharpEssentials.Results.Testing

Assertion helpers for Result, Result<T>, the typed Result<T, E1..E3>, Maybe<T> and problem details HTTP responses. A failed assertion throws ResultAssertionException, and its message lists every error with its type, code, description and metadata. The package references no test framework, so it works from xUnit (v2 or v3), NUnit, MSTest or a console check, next to any assertion library.

Installation

dotnet add package CSharpEssentials.Results.Testing

Targets net11.0, net10.0, net9.0 and netstandard2.1. It depends only on CSharpEssentials.Results and CSharpEssentials.Maybe. The problem details helpers read the body with System.Text.Json, so they need no ASP.NET Core reference.

using CSharpEssentials.ResultPattern.Testing;

Results

OrderDto order = (await service.GetAsync(id)).ShouldBeSuccess();

Error[] errors = service.Create(invalid).ShouldBeFailure();

Error error = service.Create(invalid).ShouldHaveError("Order.Name.Required");
service.Create(invalid).ShouldHaveError(ErrorType.Validation);
service.Create(invalid).ShouldHaveError(ErrorType.Validation, "Order.Name.Required");

When a result is not what the test expected, the message shows every error:

Expected a success, but the result failed with 2 errors:
  [0] Validation Name.Required: Name is required. metadata [{ field: Name }]
  [1] NotFound User.NotFound: User 7 was not found.
Member What it does
ShouldBeSuccess() On Result: passes on success. On Result<T> and the typed results: returns the value
ShouldBeFailure() Returns the errors. A typed result gives its one error converted to an Error. On success, the message shows the value of Result<T> and the typed results
ShouldHaveError(code) / (type) / (type, code) Returns the first error that matches. The code is compared ordinally
ShouldBeFailureOf<TError>() Typed results only: returns the error when the result holds a TError (a slot type or a base type of one)

default(Result<T>) fails with the Result.Default error, as at runtime. A default typed result holds no case, and the message says so.

Typed results

Result<OrderDto, NotFoundError, ConflictError> result = await service.ReserveAsync(id);

ConflictError conflict = result.ShouldBeFailureOf<ConflictError>();

When the result holds another case, the message names it:

Expected an error of type NotFoundError, but the result holds the error ConflictError { Code = Order.Taken, ... }.

Result and Result<T> have no ShouldBeFailureOf. They hold Error values, never NotFoundError or another typed error, so a type check could only match Error itself. Use ShouldHaveError(type) or ShouldHaveError(code) on them instead.

Maybe

Customer customer = repository.Find(id).ShouldHaveValue();
repository.Find(missingId).ShouldBeNone();

Problem details responses

using HttpResponseMessage response = await client.PostAsJsonAsync("/orders", new CreateOrder(""));

JsonElement problem = await response.ShouldBeValidationProblemAsync("Name.NotEmpty");

using HttpResponseMessage missing = await client.GetAsync("/orders/42");
await missing.ShouldBeProblemAsync(404, "Order.NotFound");
Member What it does
ShouldBeProblemAsync(status?, code?) Checks the application/problem+json content type, the status and an error code, and returns the body as a JsonElement. The code is looked up in errorCodes and in errors
ShouldBeValidationProblemAsync(params keys) Checks for a 400 problem whose errors has every key: the codes of a list (ValidationErrorsFormat.List) or the property names of an object (Dictionary or ByField)

The message shows the request method and URI, the status and the body:

Expected status 404, but it was 400.
POST http://localhost/orders returned 400 BadRequest with body:
{"status":400,"title":"Validation Error","errors":[{"code":"Name.NotEmpty","description":"'Name' must not be empty."}]}

The helpers read the field names that CSharpEssentials.AspNetCore writes (errorCodes, errors and the code of each list entry), so they also work for any RFC 9457 producer that uses the same names.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.0.0 0 11/28/2026