2014-05-18 6 views
0

소켓에 대한 테스트를 작성하려고합니다. 나는 passport.socketio를 사용하고 있으므로 로그인 된 사용자가 없을 때 소켓을 연결해서는 안되며 따라서 소켓 콜백은 결코 실행되지 않습니다. 나는 그것을 시험하고 싶다.mocha가 시간 초과를 예상해야합니다. 시간이 초과되면 done()을 호출합니다.

시간 초과를 실제로 예상 할 수있는 방법이 있습니까?

describe('Socket without logged in user', function() { 
    it('passport.socketio should never let it connect', function(done) { 
     socket.on('connect', function() { 
      // this should never happen, is the expected behavior. 
     }); 
    }); 
}); 

또는 다른 방법으로 접근해야합니까?

답변

1

당신은 기본적으로 스스로를 프로그램 할 수 있습니다 :

var EXPECTED_TIMEOUT = 2000; // This value should be lesser than the actual mocha test timeout. 
it('passport.socketio should never let it connect', function(done) { 
    this.timeout(EXPECTED_TIMEOUT + 100); // You add this to make sure mocha test timeout will only happen as a fail-over, when either of the functions haven't called done callback. 
    var timeout = setTimeout(done, EXPECTED_TIMEOUT); // This will call done when timeout is reached. 
    socket.on('connect', function() { 
     clearTimeout(timeout); 
     // this should never happen, is the expected behavior. 
     done(new Error('Unexpected call')); 
    }); 
}); 
또한 코드 단축 addTimeout 모듈을 사용할 수 있습니다

:

var EXPECTED_TIMEOUT = 2000; // This value should be lesser than the actual mocha test timeout. 
it('passport.socketio should never let it connect', function(done) { 
    this.timeout(EXPECTED_TIMEOUT + 100); // You add this to make sure mocha test timeout will only happen as a fail-over, when either of the functions haven't called done callback. 
    function connectCallback() { 
     done(new Error('Unexpected Call')); 
    } 
    socket.on('connect', addTimeout(EXPECTED_TIMEOUT, connectCallback, function() { 
     done() 
    }); 
});