我有以下测试说明了我试图实现的简单示例(两个等效列表的比较):[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)
1 回答
哈士奇WWW
TA贡献1799条经验 获得超6个赞
我怀疑该接口IDomainEvent
没有任何public
属性。这解释了您看到的异常,因为 中的逻辑BeEquivalentTo()
使用属性 on ,IDomainEvent
因为列表的类型为List<IDomainEvent>
。
要使用属性来比较两个列表BusinessCreatedDomainEvent
,您可以改为使用
list1.Should().BeEquivalentTo(list2, options => options.RespectingRuntimeTypes());
从文档
默认情况下,在递归比较期间选择要处理的成员时,Fluent 断言尊重对象或成员的声明(编译时)类型。也就是说,如果主体是一个 OrderDto 但它被分配给的变量具有类型 Dto,则在将对象与 order 变量进行比较时,只会考虑由后一个类定义的成员。可以配置此行为,如果您愿意,可以选择使用运行时类型:
- 1 回答
- 0 关注
- 261 浏览
添加回答
举报
0/150
提交
取消