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

标签 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 接口(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/

相关文章:

c# - 如何调用 Web 服务方法?

f# - 使用 FSUnit.Xunit 测试集合

c# - 使用 ShouldBeEquivalentTo 时如何排除 IEnumerable 中所有项目的属性?

c# - 如何使用 FluentAssertions 在 XUnit 中测试 MediatR 处理程序

c# - 使用 Fluent Assertions 库的多个断言

c# - C# (.NET 4.0) 中的快速字符串后缀检查?

c# - Entity Framework : How to set model as Serializable in Entity Framework

c# - 在 C# 中编写泛型算术

c# - Xunit 2.3.0 无法将日期作为内联参数传递

ruby - 为什么我的 Ruby 代码中出现 "wrong number of arguments (0 for 2)"异常?