我在试着写一个套接字测试。我使用的是passport.socketio,所以当没有登录用户时,套接字不应该连接(因此套接字回调不会触发)。我想测试一下。
是否有一种方法来真正期待超时?
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.
});
});
});或者我应该用其他方法来处理这个问题?
发布于 2014-05-18 09:07:23
你基本上可以自己编程:
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()
});
});https://stackoverflow.com/questions/23719916
复制相似问题