Fluent断言-在ShouldBeEquivalentTo()中重写比较
本文关键字:重写 比较 ShouldBeEquivalentTo 断言 Fluent | 更新日期: 2023-09-27 18:29:56
我有以下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>'
有人能建议正确的语法吗?
您必须告诉FA何时使用WhenTypeIs<DateTime>()
的Using
构造。换句话说:
actual.ShouldBeEquivalentTo(expected, options =>
options.Using<DateTime>(x => x.Subject.Should().BeCloseTo(DateTime.Now)).WhenTypeIs<DateTime>());
但是,我建议不要过于依赖DateTime.Now
。相反,可以考虑使用Ayende Rahien在本文中提出的方法。