流利断言 - 在ShouldBeEquivalentTo()中覆盖比较

Kev*_*zyk 3 c# fluent-assertions

我有以下DTO:

public class Dto
{
    public DateTime Date { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我试图根据FA维基使用这种语法覆盖属性的比较:

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)));
}
Run Code Online (Sandbox Code Playgroud)

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

Cannot implicitly convert type 'FluentAssertions.Equivalency.EquivalencyAssertionOptions<FluentAssertions.ShouldBeEquivalentTo.Override.Dto>.Restriction<System.DateTime>' to 'FluentAssertions.Equivalency.EquivalencyAssertionOptions<FluentAssertions.ShouldBeEquivalentTo.Override.Dto>'
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议正确的语法吗?

Den*_*men 5

你必须告诉FA何时使用该Using建筑WhenTypeIs<DateTime>().换一种说法:

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

但是,我建议不要DateTime.Now太依赖.相反,考虑像Ayende Rahien中提出了使用的东西这个文章.