简体   繁体   English

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

[英]Fluent Assertions - Overriding comparison in ShouldBeEquivalentTo()

I have the following DTO: 我有以下DTO:

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

And I'm trying to override the comparison of a property using this syntax as per the FA wiki : 我试图根据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)));
}

But the test does not compile. 但是测试没有编译。 I get this error: 我收到此错误:

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

Can anyone advise the correct syntax? 任何人都可以建议正确的语法吗?

You have to tell FA when to use that Using construction using the WhenTypeIs<DateTime>() . 你必须告诉FA时使用的是Using使用建筑WhenTypeIs<DateTime>() In other words: 换一种说法:

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

However, I would suggest not to rely on DateTime.Now too much. 但是,我建议不要依赖DateTime.Now太多了。 Instead, consider using something like Ayende Rahien has proposed in this article. 相反,考虑像Ayende Rahien中提出了使用的东西这个文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM