我正在使用Mocha为Node API编写测试.在一个测试中,我需要执行2个操作并比较每个操作的时间戳并确保它们不同.为此,我需要可靠地暂停测试执行至少一秒钟.我试图setTimeout在第二次调用之前暂停Mocha执行ping,但它没有发生.
it( 'should insert then update the timestamp.', function( done ) {
Do.ping( 'arg1', function( err, result ) {
should.not.exist( err );
setTimeout( Do.ping( 'arg1', function( err, result ) {
// Test that the timestamp of the first ping is before the timestamp
// of the second ping (among other things)
done();
}), 1000 );
});
});
Run Code Online (Sandbox Code Playgroud)
有人看到我在这里蠢蠢欲动吗?或者,是否有更好的(即更多Mocha-ish)方式来做我想做的事情?
我最终使用了Sinon 的假定时器API,它运行得很好。beforeEach()分别在和中实例化并重置假计时器afterEach()。在实际测试中,只需按照您需要的方式提前时钟即可:
clock.tick( 180000 ); // Advances the JS clock 3 minutes (180 seconds)
Run Code Online (Sandbox Code Playgroud)