gpt4 book ai didi

c# - FluentAssertions Should().BeEquivalentTo() 失败,列表包含从相同接口(interface)派生的运行时指定类型

转载 作者:行者123 更新时间:2023-11-30 19:04:27 32 4
gpt4 key购买 nike

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

[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 接口(interface)派生的任意数量的不同类型的项目。目前测试失败。我怀疑它与 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

最佳答案

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

使用 BusinessCreatedDomainEvent 上的属性比较两个列表相反,您可以使用

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

来自documentation

By default, Fluent Assertions respects an object’s or member’s declared (compile-time) type when selecting members to process during a recursive comparison. That is to say if the subject is a OrderDto but the variable it is assigned to has type Dto only the members defined by the latter class would be considered when comparing the object to the order variable. This behavior can be configured and you can choose to use run-time types if you prefer:

关于c# - FluentAssertions Should().BeEquivalentTo() 失败,列表包含从相同接口(interface)派生的运行时指定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51846837/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com