1
Env : Mocha, Sinon, node.js. 왜이 테스트가 8ms에 실행될 때 5500ms의 시간 초과가 있습니까? 어쩌면 나는 타이머가 무엇인지 이해하지 못하겠습니까? 내 말은, 실행을 잘못한 것인가? 타이머가 끝날 때까지 시험을 끝내지 마십시오.sinon.useFakeTimer()가있는 경우에도 TimeOut이 작동하지 않습니다.
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;
});
'sinon.useFakeTimers()'는 타이머의 타임 아웃 값 이상으로 가짜 시계가 틱되면 자동으로 (즉시) 콜백을 실행하는 버전으로 전역 setTimeout 함수를 대체합니다. 그래서 테스트가 빠르게 진행됩니다. 그리고 좋은 일이기 때문에 단위 테스트가 테스트 당 5 초가 걸리는 것을 원하지 않습니다. – blah238