Home > Enterprise >  FluentAssertions Equivalency Comparison Behavior and IMemberInfo
FluentAssertions Equivalency Comparison Behavior and IMemberInfo

Time:11-03

I am using FluentAssertions (v6.2.0) to test API's that return table-like data. I want to change comparison behavior for one of the field, and tried to use method described in documentation.

orderDto.Should().BeEquivalentTo(order, options => options
    .Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Seconds()))
    .When(info => info.Name == "Date"));

The issue is that IMemberInfo class that When extension method is expecting doesn't have Name property, it has property called Path. Was Name replaced by Path and this is a typo in documentation, or do I need to import another namespace to use Name property?

CodePudding user response:

From a quick look at the FluentAssertions source code, I'm seeing that the info argument is of type IObjectInfo and it has a Path property. A quick test with this code shows the Path property working as you would expect:

void Main()
{
    var orderDto = new OrderDto { Date = DateTime.Now };
    var order = new Order { Date = DateTime.Now };
    //var order = new Order { Date = DateTime.Now.Subtract(TimeSpan.FromSeconds(2)) };

    orderDto.Should().BeEquivalentTo(order, options => options
        .Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Seconds()))
        .When(info => info.Path == "Date"));
}

class OrderDto {
    public DateTime Date { get; set; }
}

class Order
{
    public DateTime Date { get; set; }
}

In fact, the FluentAssertions test for that code also uses path. See https://github.com/fluentassertions/fluentassertions/blob/master/Tests/FluentAssertions.Specs/Equivalency/ExtensibilityRelatedEquivalencySpecs.cs#L394

There is also an IMethodInfo interface with both Name and Path properties. However, that is used by the Include* and Exclude* methods.

So it appears to be a documentation bug.

  • Related