FluentAssertions Should()。BeEquivalentTo()失败,其中包含从相同接口派生的运行时指定类型的列表

时间:2018-08-14 17:27:40

标签: c# xunit fluent-assertions

我进行了以下测试,展示了我试图实现的简单示例(两个等效列表的比较):

[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)
[8/14/2018 5:21:26 PM Informational] [xUnit.net 00:00:00.7946610]         C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Collections\CollectionAssertions.cs(394,0): at FluentAssertions.Collections.CollectionAssertions`2.BeEquivalentTo[TExpectation](IEnumerable`1 expectation, Func`2 config, String because, Object[] becauseArgs)
[8/14/2018 5:21:26 PM Informational] [xUnit.net 00:00:00.7947629]         C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Collections\CollectionAssertions.cs(331,0): at FluentAssertions.Collections.CollectionAssertions`2.BeEquivalentTo[TExpectation](IEnumerable`1 expectation, String because, Object[] becauseArgs)
[8/14/2018 5:21:26 PM Informational] [xUnit.net 00:00:00.7948569]         C:\Users\Christian\source\repos\unify\src\Services\Business\BusinessWrite\BusinessWrite.UnitTests\Infrastructure\EventStoreUnitTests.cs(43,0): at BusinessWrite.UnitTests.Infrastructure.EventStoreUnitTests.Test()
[8/14/2018 5:21:26 PM Informational] [xUnit.net 00:00:00.8058517]   Finished:    BusinessWrite.UnitTests

1 个答案:

答案 0 :(得分:8)

我怀疑接口IDomainEvent没有任何public属性。 这就解释了您所看到的异常,因为BeEquivalentTo()中的逻辑使用IDomainEvent上的属性,因为列表的类型为List<IDomainEvent>

要改为使用BusinessCreatedDomainEvent上的属性比较两个列表,可以改用

list1.Should().BeEquivalentTo(list2, options => options.RespectingRuntimeTypes());

documentation

  

默认情况下,在递归比较中选择要处理的成员时,Fluent断言默认使用对象或成员的声明(编译时)类型。也就是说,如果主题是OrderDto,但将其分配给的变量具有Dto类型,则在将对象与order变量进行比较时,仅考虑由后一类定义的成员。可以配置此行为,并且您可以根据需要选择使用运行时类型: