【问题标题】:FluentAssertions Equivalency Comparison Behavior and IMemberInfoFluentAssertions 等价比较行为和 IMemberInfo
【发布时间】:2021-12-17 10:04:23
【问题描述】:

我正在使用 FluentAssertions (v6.2.0) 来测试返回类似表格数据的 API。我想更改其中一个字段的比较行为,并尝试使用documentation 中描述的方法。

orderDto.Should().BeEquivalentTo(order, options => options
    .Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Seconds()))
    .When(info => info.Name == "Date"));

问题是When 扩展方法所期望的IMemberInfo 类没有Name 属性,它具有名为Path 的属性。 Name 是否被 Path 替换,这是文档中的拼写错误,还是我需要导入另一个命名空间才能使用 Name 属性?

【问题讨论】:

    标签: c# fluent-assertions


    【解决方案1】:

    快速查看FluentAssertions source code,我发现info 参数的类型为IObjectInfo,并且它具有Path 属性。使用此代码进行的快速测试显示 Path 属性按预期工作:

    void Main()
    {
        var orderDto = new OrderDto { Date = DateTime.Now };
        var order = new Order { Date = DateTime.Now };
        //var order = new Order { Date = DateTime.Now.Subtract(TimeSpan.FromSeconds(2)) };
    
        orderDto.Should().BeEquivalentTo(order, options => options
            .Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Seconds()))
            .When(info => info.Path == "Date"));
    }
    
    class OrderDto {
        public DateTime Date { get; set; }
    }
    
    class Order
    {
        public DateTime Date { get; set; }
    }
    

    事实上,该代码的 FluentAssertions 测试也使用路径。见https://github.com/fluentassertions/fluentassertions/blob/master/Tests/FluentAssertions.Specs/Equivalency/ExtensibilityRelatedEquivalencySpecs.cs#L394

    还有一个具有NamePath 属性的IMethodInfo 接口。但是,Include*Exclude* 方法使用它。

    所以它似乎是一个文档错误。

    【讨论】:

    • 谢谢你,杰克!我在代码中使用了Path。这个问题更像是 FluentAssertions 作者的“文档错字”报告,因为它并不是一个真正的错误,而且 FluentAssertions 的 Github 问题创建模板声明在 SO 而不是在 Github 上提出这样的问题。
    • 修复了文档。
    猜你喜欢
    • 2016-12-29
    • 2019-07-14
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 2010-12-25
    • 2021-12-18
    • 2014-10-27
    • 2021-01-03
    相关资源
    最近更新 更多