如何使用模拟日常程序的模拟对象? [英] How to use mock object mimicing a daily routine program?
问题描述
我的程序有一个日常例行程序,类似于闹钟事件.假设是下午2点(时间是我电脑中的系统时间),请为我做点事情.
My program has a daily routine, similar to an alarm clock event. Say, when it's 2pm(The time is the system time in my pc), do something for me.
我想做的是加快测试周期(我真的不想等待4天查看日常工作并检查错误.)我在
What I want to do is to speed up the testing period(I don't really want to wait 4 days looking at the daily routine and check errors.) I read on wiki of Mock object, the writer DID mention alarm clock program. I was so happy to see but still, don't know how to do it.
我是Mock Object的新手,我正在用Java编程.因此,JMock或EasyMock(或任何类似产品)对我来说可能还可以.
I am new to Mock Object, and I am programming in Java. So JMock or EasyMock(or any similar) might okay to me.
谢谢
推荐答案
每当需要获取当前时间时,请勿直接使用系统时钟-使用如下接口:
Whenever you need to get the current time, don't use the system clock directly - use an interface such as:
public interface Clock
{
long currentMillis();
}
然后,您可以使用系统时钟来实现此目标,以进行生产,并通过伪造品进行测试,在其中您可以将伪造品设置为所需的任何时间.
You can then implement this with a system clock for production, and pass in a fake for tests, where you can set the fake to whatever time you want.
但是,您还需要模拟驱动系统的任何因素-是您明确地等待特定时间,还是其他东西调用了您的代码?
However, you'll also need to mock out whatever's driving your system - are you explicitly waiting for a particular time, or does something else call your code?
这篇关于如何使用模拟日常程序的模拟对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!