2

I have read through FluenAssertions Extensibility page, but I still cannot understand how to create a Should().BeEquivalentTo() comparison method for collection of objects, if I already implemented Should().BeEquivalentTo() for an object, while preserving the all other assertion methods for the same collection.

Here is a simplified example: The class that I want to check:

public class CalculationResult
{
    public DateTime Date { get; set; }
    public int Id { get; set; }
    public bool CalcState { get; set; }
    public double Result { get; set; }
}

The class I convert a table row in Specflow test suite

public class CalcResultBdd
{
    public DateTime Date { get; set; }
    public double Result { get; set; }
}

The class to class comparison class is:

public class CalculationResultAssertions : ReferenceTypeAssertions<CalculationResult, CalculationResultAssertions>
{
    public CalculationResultAssertions(CalculationResult instance)
    {
        Subject = instance;
    }

    protected override string Identifier => "CalculationResult";

    [CustomAssertion]
    public AndConstraint<CalculationResultAssertions> BeEquivalentTo(
        CalcResultBdd expectedResult,
        double precision = 0.001,
        string because = "",
        params object[] becauseArgs)
    {
        Execute.Assertion.
            BecauseOf(because, becauseArgs).
                ForCondition(expectedResult != null).
                FailWith("You cannot assert that calculation result is correct if you do not pass a proper object").
            Then.
                ForCondition(precision >= 0).
                FailWith("You cannot compare double values if you provide negative precision").
            Then.
            Given(() => Subject).
                ForCondition(s => s.CalcState).
                FailWith("Expected CalcState to be \"true\"").
            Then.
                ForCondition(s => s.Date == expectedResult.Date).
                FailWith(
                    "Expected Date to be {0}{reason}, but found {1}",
                    _ => expectedResult.Date.ToString("yyyy-MM-dd"),
                    s => s.Date.ToString("yyyy-MM-dd")).
            Then.
                ForCondition(s => Math.Abs(s.Result - expectedResult.Result) <= precision).
                FailWith(
                    "Expected Result to be {0} with precision {1}{reason}, but found {2}",
                    _ => expectedResult.Result,
                    _ => precision,
                    s => s.Result);

        return new AndConstraint<CalculationResultAssertions>(this);
    }
}

Static class linking FluentAssertions and custom assertions class:

public static class AssertionExtensions
{
    public static CalculationResultAssertions Should(this CalculationResult instance)
    {
        return new CalculationResultAssertions(instance);
    }
}

If I run the following, without redefining the BeEquivalentTo for List<CalculationResult>

List<CalculationResult> actualResults = /* getting results here */
actualResults.Should().BeEquivalentTo(new CalcResultBdd[] { /* expected results here */ })

FluentAssertions will use build-in structural comparison instead of custom comparison. Is it possible for me to change something in my code so that Fluent assertions uses the custom comparison instead?

GKalnytskyi
  • 579
  • 7
  • 16

1 Answers1

0

If I'm not mistaken, the BeEquivalentTo extension method expects is generic, and will also supersede yours.

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • I suppose you are right. FluentAssertions library doesn't look up custom comparison implementation (`Be`, or `BeEquivalentTo`) from outside the library. Or I completely missed the way of registering custom implementations with the library. – GKalnytskyi Feb 16 '19 at 02:30
  • There is no concept of registering anything. It all relies on the compiler picking up the right overload. – Dennis Doomen Feb 20 '19 at 20:10