Skip to content
Michael Altmann edited this page Aug 4, 2022 · 14 revisions

FluentResults

Nuget downloads Nuget License: MIT

Full docu see readme

FluentResults.Extensions.FluentAssertions

Nuget downloads Nuget License: MIT

A small package to assert FluentResult result objects with FluentAssertions in an elegant way.

Asserting Methods

Result should be failed

var actualResult = Result.Fail("Error 1");
actualResult.Should().BeFailure();

Result should be success

var actualResult = Result.Ok();
actualResult.Should().BeSuccess();

Result should have specific value

var actualResult = Result.Ok(1);
actualResult.Should().HaveValue(1);

Result should have a specific reason

var actualResult = Result.Ok(1);
actualResult.Should().HaveReason("Error 1");
actualResult.Should().HaveReason<CustomError>("Error 2");
actualResult.Should().HaveReason(new { ... });

If you don't set an ErrorMessageComparison via parameter then the default comparison is used. The default comparison is set globally via FluentResultAssertionsConfig.ErrorMessageComparison which is by default ErrorMessageComparisonLogics.Equal. The comparison ErrorMessageComparisonLogics.ActualContainsExpected which is the default comparison logic for FluentResults.Extensions.FluentAssertions < v2.0 is also available and can be set globally or via parameter. If you need some another custom comparison logic feel free to set your own. The comparison is only a Func<string, string, bool> with the parameters actual error message and expected error message.

var actualResult = Result.Ok(1);
actualResult.Should().HaveReason("Error 1", ErrorMessageComparisonLogics.ActualContainsExpected);

Result should satisfy conditions (Experimantal)

var actualResult = Result.Ok(1);
actualResult.Should().BeFailure()
                     .And.Satisfy(result => {
                         result.Errors.Should().ContainEquivalentOf(new Error("Error 1"));
                      });