环境:摩卡,诗乃,node.js.为什么这个测试在8毫秒内执行,当时有5500毫秒的时间?也许我不明白什么是计时器?我的意思是,它应该扼杀执行权吗?在计时器结束之前没有完成测试。
var sinon = require('sinon');
var chai = require('chai');
expect = chai.expect;
should = chai.should();
assert = chai.assert;
var clock;
beforeEach(function () {
clock = sinon.useFakeTimers();
});
afterEach(function () {
clock.restore();
});
it("should time out after 5000 ms", function() {
var timedOut = false;
setTimeout(function () {
timedOut = true;
}, 5000);
timedOut.should.be.false;
clock.tick(5500);
timedOut.should.be.true;
});
答案 0 :(得分:-1)
我找到了使用setTimeOut在mocha中为node.js工作的方法。
// We need to pass the mocha's function 'done' that tells mocha to wait till this function is called
it('It should wait around 5 seconds', function(done){
// We augment mocha's timeout to more than default 2000ms
// we put more than the 5 seconds of setTimeout to asure us
this.timeout(6 * 1000);
setTimeout(function(){
// tells mocha we are finished
done();
}, 5*1000);
});
这是茉莉花中“等待”功能的替代品。