我正在构建一个计时器类,但我无法通过测试。我想测试当计时器上的时间过去时调用一个方法。
我有以下课程:
public class TimeOutTimer
{
private readonly ISubscriber _subscriber;
private Timer _timer;
public TimeOutTimer(ISubscriber subscriber)
{
_subscriber = subscriber;
}
public void Start()
{
_timer = new Timer(1000);
_timer.Start();
_timer.Elapsed += TimerOnElapsed;
}
private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
_subscriber.TimeReached();
}
}
和测试,使用最小起订量:
[Test]
public void Start_WithValidParameters_TriggersTimeReached()
{
var subscriberMock = new Mock<ISubscriber>();
var timer = new TimeOutTimer(subscriberMock.Object);
timer.Start();
subscriberMock.Verify(subscriber => subscriber.TimeReached());
}
如果我取出 Timer 并直接调用 _subscriber.TimeReached(),它就可以工作。
难道我做错了什么?