用于测试的静态日期时间

发布于 2024-08-13 21:48:14 字数 167 浏览 1 评论 0原文

我们有一个日期时间服务,可以在需要使用日期时拉取数据库日期。现在,如果我想使用 FitNesse 使用“今天”日期运行测试,创建 (OurService).CurrentDate 静态的最简单方法是什么?

我们有一个控件,可以将天、年或月广告到当前日期..我希望能够对此进行测试。

谢谢!

We have a DateTime Service that pulls the Database date down when we need to use a Date. Now, If I want to run a test using "Todays" Date using FitNesse, What would be the easiest way of creating a way to have (OurService).CurrentDate be static?

We have a control that ads Days,Years, or Months to the current date.. and I would like to be able to test this.

Thanks!

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

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

发布评论

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

评论(1

初相遇 2024-08-20 21:48:14

在您的代码中,添加一个方法以从“某处”提取日期(“某处”是实现细节)。例如,在我的代码中:

public class X {
    Date now = new Date ();
}

替换为:

public class X {
    Date now = now ();
    protected Date now () { return new Date (); }
}

我现在可以在测试中扩展 X,以使用返回固定日期的内容覆盖 now()

还要添加一个测试,该测试调用原始实现但忽略结果(好吧,您可以检查结果是否在“现在”的范围内)。此测试只是确保旧的日期拉取代码继续工作。

In your code, add a method to pull the date from "somewhere" ("somewhere" is an implementation detail). For example, in my code:

public class X {
    Date now = new Date ();
}

is replaced with:

public class X {
    Date now = now ();
    protected Date now () { return new Date (); }
}

I can now extend X in my tests to override now() with something that returns a fixed date.

Also add a test which calls the original implementation but which ignores the result (well, you can check that the result is within a range of "now"). This test just makes sure that the old date pulling code continues to work.

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