【问题标题】:FluentAssertions won't exclude missing members on object graph comparisonFluentAssertions 不会在对象图比较中排除缺失的成员
【发布时间】:2022-01-09 19:35:25
【问题描述】:

我正在尝试使用 FluentAssertions object graph comparison 将 DTO 与其各自的实体进行比较。我的实体有一些我的 DTO 没有的额外元数据属性。

我正在尝试使用 ExcludingMissingMembers 选项排除这些额外的属性,甚至通过像这样单独排除每个成员来明确排除这些属性:

result.Entity.Should().BeEquivalentTo(dto, opt => opt
  .ExcludingMissingMembers()
  .Excluding(x => x.ValidationResult)
  .Excluding(x => x.CreatedBy)
  .Excluding(x => x.CreatedAt));

但是由于我的 DTO 没有的额外属性,我的测试一直失败。

消息:预期的结果。实体(实体类型)为

实体
{
CreatedAt =
创建者 =
FinancialResourcesOrigins = {劳工,劳工}
ProductsOfInterest = {FixedIncome, FixedIncome}
验证结果 =
},但找到了

Dto
{
FinancialResourcesOrigins = {劳工,劳工}
ProductsOfInterest = {FixedIncome, FixedIncome}
}

有配置:

  • 使用声明的类型和成员
  • 按值比较枚举
  • 排除成员ValidationResult
  • 排除成员 CreatedBy
  • 排除成员 CreatedAt
  • 按名称匹配成员(或抛出)
  • 严格控制字节数组中的项目顺序
  • 没有自动转换。

我在这里错过了什么?

【问题讨论】:

    标签: c# .net fluentvalidation fluent-assertions


    【解决方案1】:

    所以,一个快速的测试结果:

    var entity = new
        {
            CreatedAt = DateTime.Now,
            CreatedBy = "",
            FinancialResourcesOrigins = new[] { "Labor", "Labor" },
            ProductsOfInterest = new[] { "FixedIncome", "FixedIncome" },
            ValidationResult = ""
        };
    
    var dto = new
        {
            FinancialResourcesOrigins = new[] { "Labor", "Labor" },
            ProductsOfInterest = new[] { "FixedIncome", "FixedIncome" },
        };
    
    
    entity.Should()
         .BeEquivalentTo(dto, opt => opt
         .ExcludingMissingMembers());
    

    这应该可以按预期工作。

    您的错误似乎是显式排除,因为成员(您的变量 x)应该来自您的 DTO,但它们不存在

    【讨论】:

    • 我只在使用ExcludingMissingMenbers() 进行测试后才添加了显式排除,并且它返回了相同的确切错误。所以你的回答不能解决我的问题。即使仅使用显式排除也会导致相同的错误。
    • 您能添加您的完整对象表示吗?
    猜你喜欢
    • 2021-01-11
    • 2018-09-02
    • 2017-01-16
    • 2013-06-08
    • 1970-01-01
    • 2016-12-22
    • 2020-05-21
    • 2016-05-07
    • 2013-11-11
    相关资源
    最近更新 更多