2014-11-18 2 views
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; 
}); 
+0

'sinon.useFakeTimers()'는 타이머의 타임 아웃 값 이상으로 가짜 시계가 틱되면 자동으로 (즉시) 콜백을 실행하는 버전으로 전역 setTimeout 함수를 대체합니다. 그래서 테스트가 빠르게 진행됩니다. 그리고 좋은 일이기 때문에 단위 테스트가 테스트 당 5 초가 걸리는 것을 원하지 않습니다. – blah238

답변

-1

node.js에 대해 mocha에서 setTimeOut을 만드는 방법을 발견했습니다.

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

이것은 재스민의 '대기'기능을 대체합니다.

관련 문제