3

我有以下 DTO:

public class Dto
{
    public DateTime Date { get; set; }
}

而且我正在尝试根据FA wiki使用此语法覆盖属性的比较:

public void Override_test()
{
    // Arrange
    var actual = new Dto { Date = DateTime.Now };
    var expected = new Dto { Date = DateTime.Now };

    // Act

    // Assert
    actual.ShouldBeEquivalentTo(expected, options => 
        options.Using<DateTime>(x => x.Subject.Should().BeCloseTo(DateTime.Now)));
}

但是测试没有编译。我收到此错误:

Cannot implicitly convert type 'FluentAssertions.Equivalency.EquivalencyAssertionOptions<FluentAssertions.ShouldBeEquivalentTo.Override.Dto>.Restriction<System.DateTime>' to 'FluentAssertions.Equivalency.EquivalencyAssertionOptions<FluentAssertions.ShouldBeEquivalentTo.Override.Dto>'

谁能建议正确的语法?

4

1 回答 1

7

您必须告诉 FA 何时Using使用WhenTypeIs<DateTime>(). 换句话说:

actual.ShouldBeEquivalentTo(expected, options => 
    options.Using<DateTime>(x => x.Subject.Should().BeCloseTo(DateTime.Now)).WhenTypeIs<DateTime>());

不过,我建议不要DateTime.Now过分依赖。相反,请考虑使用 Ayende Rahien 在本文中提出的类似方法

于 2014-11-11T20:25:16.383 回答