C#、NUnit:是否可以测试一个 DateTime 是否与另一个 DateTime 非常接近,但不一定相等?

发布于 2024-08-12 04:21:06 字数 359 浏览 4 评论 0原文

假设我有这个测试:

[Test]
public void SomeTest()
{
    var message = new Thing("foobar");
    Assert.That(thing.Created, Is.EqualTo(DateTime.Now));
}

例如,这可能会导致 Thing 的构造函数失败,需要一些时间。是否有某种 NUnit 构造允许我指定 Created 时间不必完全等于 DateTime.Now ,例如只要在一秒之内?

是的,我知道构造函数不应该花费太多时间,但只是作为一个例子:p

Say I have this test:

[Test]
public void SomeTest()
{
    var message = new Thing("foobar");
    Assert.That(thing.Created, Is.EqualTo(DateTime.Now));
}

This could for example fail the constructor of Thing took a bit of time. Is there some sort of NUnit construct that would allow me to specify that the Created time don't have to be exactly equal to DateTime.Now, as long as it for example is within one second of it?

And yes I know constructors are not supposed to take much time, but just as an example :p

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

各自安好 2024-08-19 04:21:06

我还没有尝试过,但根据文档看起来这应该可行:

Assert.That(thing.Created, Is.EqualTo(DateTime.Now).Within(1).Minutes);

我不能说我通常是约束系统的粉丝 - 我是一个 Assert.AreEqual 粉丝 - 但这个特定的构造相当简洁。

(作为一个原则,我应该指出,您最好将某种“时钟”接口作为依赖项传递,然后就不会出现任何不准确的情况。您可以在测试中伪造它,并使用用于生产的系统时钟。)

I haven't tried it, but according to the docs it looks like this should work:

Assert.That(thing.Created, Is.EqualTo(DateTime.Now).Within(1).Minutes);

I can't say I'm normally much of a fan of the constraints system - I'm an Assert.AreEqual fan - but that particular construct is rather neat.

(As a point of principle I should remark that you'd be best off passing some sort of "clock" interface in as a dependency, and then you wouldn't have any inaccuracy. You could fake it for the tests, and use the system clock for production.)

娜些时光,永不杰束 2024-08-19 04:21:06

检查 TimeSpan 对象 - 使用 TimeSpan 比较两个日期并检查值是否在阈值内。

TimeSpan span = thing.Created - DateTime.Now;
if(span.TotalSeconds <= 1)
   [..]

Check out the TimeSpan object - compare both dates using TimeSpan and check to see if the values are within your threshold.

TimeSpan span = thing.Created - DateTime.Now;
if(span.TotalSeconds <= 1)
   [..]
水染的天色ゝ 2024-08-19 04:21:06

您需要从定义“非常接近”开始。如果“非常接近”指的是几百个刻度,那么您可以这样做:

Assert.That(200, Is.LessThanOrEqualTo(DateTime.Now.Ticks - thing.Created.Ticks));

然后只要您的日期在 200 个刻度以内,您的测试就会通过。

You'd need to start out by defining "very close". If by "very close" you mean a few hundred ticks then you could do this:

Assert.That(200, Is.LessThanOrEqualTo(DateTime.Now.Ticks - thing.Created.Ticks));

Then whenever you've got dates within 200 ticks your test passes.

冷了相思 2024-08-19 04:21:06

您可以检查减去它们并检查时间跨度。

        DateTime.Now.Subtract(thing.Created).TotalSeconds

为您提供总秒数。

You could check subtract them and check the timespan.

        DateTime.Now.Subtract(thing.Created).TotalSeconds

Gives you the totalseconds.

素罗衫 2024-08-19 04:21:06

您始终可以减去 DateTime 变量并得到 TimeSpan。有了时间跨度,你就可以做任何你想做的事情。

You can always subtract DateTime variables and you get TimeSpan. With Time span you can do whatever you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文