【发布时间】:2021-01-31 11:01:23
【问题描述】:
我一直在搞乱 c#9 的一些新功能,但我遇到了一些不太有趣的事情。如果我尝试在具有 DateTimeOffset 的记录上使用 Should().BeEquivalentTo(),将 DateTimeOffset 的 Using 选项设置为使用 BeCloseTo,即使 DateTimeOffset 在允许的精度范围内,测试也会失败。有没有办法让它工作(不将记录更改为类哈哈)?非常感谢!
例子:
public class ExampleTest
{
[Fact]
public void FunWithRecords()
{
var thing1 = new Thing
{
SomeDate = DateTimeOffset.UtcNow
};
var thing2 = new Thing
{
SomeDate = DateTimeOffset.UtcNow.AddSeconds(2)
};
thing1.Should().BeEquivalentTo(thing2, o =>
o.Using<DateTimeOffset>(ctx =>
ctx.Subject.Should().BeCloseTo(ctx.Expectation, 10000))
.WhenTypeIs<DateTimeOffset>());
}
}
public record Thing
{
public DateTimeOffset SomeDate {get; init;}
}
【问题讨论】:
标签: c# unit-testing datetime fluent-assertions