gpt4 book ai didi

mocha.js - 使用 setTimeout() 暂停 Mocha 测试

转载 作者:行者123 更新时间:2023-12-04 03:30:08 25 4
gpt4 key购买 nike

我正在使用 Mocha 为 Node API 编写测试。在一项测试中,我需要执行 2 个操作并比较每个操作的时间戳并确保它们不同。为此,我需要可靠地暂停测试执行至少一秒钟。我试图使用 setTimeout在第二次调用 ping 之前暂停执行 Mocha ,但它没有发生。

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 );
});
});

有没有人看到我在这里无聊?或者,有没有更好的(即更 Mocha 的)方式来做我想做的事情?

最佳答案

我最终使用了 Sinon 的假计时器 API,并且效果很好。在 beforeEach() 中实例化并重置假定时器和 afterEach() , 分别。在实际测试中,只需以您需要的任何方式提前时钟:

clock.tick( 180000 ); // Advances the JS clock 3 minutes (180 seconds)

关于mocha.js - 使用 setTimeout() 暂停 Mocha 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17888405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com