如何测试一个功能,依赖于日期的事件发生呢? [英] How to test a feature that depends on date events happen?

查看:177
本文介绍了如何测试一个功能,依赖于日期的事件发生呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们怎样才能使一个功​​能,依赖于日期的东西验收/集成测试会怎么样?

How can we make an acceptance/integration test on a feature that depends on date something happens?

为了简单起见,我们假设这样的特征:我们监视的文件夹,并添加项目到相应的接收到的文件一个ListView。列表视图项目的颜色取决于它收到的日期。再说了,如果它收到上周日,颜色为红色,星期一,它是蓝色等。

For simplicity, let's assume this feature: we monitor a folder, and add items to a ListView corresponding to the received file. The colour of the listview item will depend on the date it is received. Say, if it's received on sunday, the colour is red, monday, it's blue, etc.

我们如何才能使此功能的测试code,而不需要一个星期来运行?如果测试code修改系统日期(但我怕这会导致古怪的测试框架和报告)?有关信息,该应用程序是一个.NET的开发人员使用DateTime.Now得到接收时间。

How can we make an test code on this feature without needing a week to run? Should the test code modify system date (But I'm afraid this will cause weirdness in the testing framework and report)? For information, the application is a .net and the developer uses DateTime.Now to get the receive time.

注意,这不是一个单元测试,这是一个验收测试是模拟用户交互。

Note that this is not a unit test, this is an acceptance test that mimics the user interaction.

推荐答案

  • 您将不得不使用一个假的框架,可以存根什么DateTime.Now返回而不需要一个接口。如 TypeMock
  • 否则,您可以创建你有更多的控制自己的Date对象伪造(这是在这种情况不太理想,因为它只是正在做测试你的code)
  • 否则,你可以创建一个具有GetCurrentDate方法,你可以存根类。在您的督促落实,你可以用DateTime.Now,但在您的测试,你可以存根出来,使其返回任何日期你想要的。 这可能是最好的解决办法IMO

  • You will have to use a Fake Framework that can stub what DateTime.Now returns without needing an interface. Like TypeMock
  • Otherwise, you could create your own Date object that you have more control over faking (this is less desirable in this case as it is only being done to test your code)
  • Otherwise, you could create a class that has a GetCurrentDate method that you can stub. In your prod implementation you can use DateTime.Now, but in your test, you can stub it out so that it returns whatever date you want. This is probably the best solution IMO
  • 不过,这并不会测试完整的,真正的实现,是更多的是单元测试。如果你确实是在谈论端到端的集成测试,那么,你就需要修改系统时间。

    However, this will not test the full, true implementation and is more of a unit test. If you are indeed talking about end to end integration testing, then yes, you will need to modify the system time.

    这篇关于如何测试一个功能,依赖于日期的事件发生呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆