FluentAssertions Should().BeEquivalentTo() 失败

我有以下测试说明了我试图实现的简单示例(两个等效列表的比较):


[Fact]

public void Test()

{

    // Arrange

    var list1 = new List<IDomainEvent> { new BusinessCreatedDomainEvent { Name = "Microsoft" } };

    var list2 = new List<IDomainEvent> { new BusinessCreatedDomainEvent { Name = "Microsoft" } };


    // Act


    // Assert

    list1.Should().BeEquivalentTo(list2);

}

理想情况下,每个列表应该能够包含来自IDomainEvent界面的任意数量的不同类型的项目。目前测试失败。我怀疑这与Should().BeEquivalentTo不适合比较运行时指定的类型有关。我如何配置这个例子,以便测试通过?


目前测试结果如下:


[8/14/2018 5:21:26 PM Error] [xUnit.net 00:00:00.7909767]     BusinessWrite.UnitTests.Infrastructure.EventStoreUnitTests.Test [FAIL]

[8/14/2018 5:21:26 PM Informational] [xUnit.net 00:00:00.7922005]       System.InvalidOperationException : No members were found for comparison. Please specify some members to include in the comparison or choose a more meaningful assertion.

[8/14/2018 5:21:26 PM Informational] [xUnit.net 00:00:00.7932461]       Stack Trace:

[8/14/2018 5:21:26 PM Informational] [xUnit.net 00:00:00.7941986]         C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Equivalency\GenericEnumerableEquivalencyStep.cs(74,0): at FluentAssertions.Equivalency.GenericEnumerableEquivalencyStep.Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)

[8/14/2018 5:21:26 PM Informational] [xUnit.net 00:00:00.7945426]         C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Equivalency\EquivalencyValidator.cs(85,0): at FluentAssertions.Equivalency.EquivalencyValidator.AssertEqualityUsing(IEquivalencyValidationContext context)

[8/14/2018 5:21:26 PM Informational] [xUnit.net 00:00:00.7946187]         C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Equivalency\EquivalencyValidator.cs(38,0): at FluentAssertions.Equivalency.EquivalencyValidator.AssertEquality(EquivalencyValidationContext context)


慕后森
浏览 262回答 1
1回答

哈士奇WWW

我怀疑该接口IDomainEvent没有任何public属性。这解释了您看到的异常,因为 中的逻辑BeEquivalentTo()使用属性 on ,IDomainEvent因为列表的类型为List<IDomainEvent>。要使用属性来比较两个列表BusinessCreatedDomainEvent,您可以改为使用list1.Should().BeEquivalentTo(list2,&nbsp;options&nbsp;=>&nbsp;options.RespectingRuntimeTypes());从文档默认情况下,在递归比较期间选择要处理的成员时,Fluent 断言尊重对象或成员的声明(编译时)类型。也就是说,如果主体是一个 OrderDto 但它被分配给的变量具有类型 Dto,则在将对象与 order 变量进行比较时,只会考虑由后一个类定义的成员。可以配置此行为,如果您愿意,可以选择使用运行时类型:
打开App,查看更多内容
随时随地看视频慕课网APP