【发布时间】:2016-04-29 00:52:26
【问题描述】:
我的程序是一个简单的计时器(它是番茄钟)。我正在尝试将单元测试整合到我的个人项目中。
我在每个计时器类的 run() 方法上运行我的测试,并断言 endTime - startTime 在我设置计时器的时间的 1 秒内。
MyTimer.run() 调用 Thread.sleep(timerMilliseconds)。
我的单元测试通过的时间少于每个计时器设置的时间。
这怎么可能?
例如。休息时间设置为 5 秒,单元测试通过
[TestMethod]
public void testRun () {
TimeSpan fiveSeconds = new TimeSpan(5000);
Rest rest = new Rest(fiveSeconds, null);
TimeSpan startTime = DateTime.Now.TimeOfDay;
try {
rest.run();
}
catch (Exception e) {
Assert.Fail(e.ToString());
}
Assert.AreEqual(startTime.Add(fiveSeconds).TotalMilliseconds, DateTime.Now.TimeOfDay.TotalMilliseconds, 1000 , "Actual sleep time not expected");
}
public class Rest : Session {
private TimeSpan timeSpan;
public override void run () {
Thread.Sleep(base.getTimer().Milliseconds); **//SOLVED: SHOULD READ base.getTimer().TotalMilliseconds**
}
public Rest (TimeSpan timeSpan, Cycle cycle) : base(timeSpan, cycle) {
this.timeSpan = timeSpan;
}
}
public abstract class Session {
private readonly TimeSpan timer;
protected Cycle cycle;
public TimeSpan getTimer () {
return this.Timer;
}
protected Session (TimeSpan minutes, Cycle cycle) {
this.timer = minutes;
this.cycle = cycle;
}
public abstract void run ();
}
【问题讨论】:
-
'now' 是该变量的糟糕名称。当您使用它时,它不再是“现在”。 startTime 不会那么混乱。
-
base.getMinutes()返回的是什么? 5 秒将产生 0 分钟,其中的Milliseconds也将是 0,所以我认为这是无需等待即可运行。 -
@Cory 这是一个命名不佳的变量。 base.getMinutes() 返回一个在创建时初始化的 TimeSpan,而不是 TimeSpan.Minutes 属性。在发布帮助之前,我应该重构我的变量名。对不起
-
什么是“休息”?我以为它是 .Net 类型,但显然不是。由于是代码行为不端,因此种类很重要的是要弄清楚它实际上是什么。
-
@DougClark:没问题,只是好奇。另一件事是你可能会在你的断言中允许一些容差,比如 10-20 毫秒。我不知道它是否会精确匹配到毫秒,但它可能会接近。
标签: c# .net visual-studio unit-testing