首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mocha应该期望超时,超时时调用done()

mocha应该期望超时,超时时调用done()
EN

Stack Overflow用户
提问于 2014-05-18 08:19:29
回答 1查看 2.6K关注 0票数 2

我在试着写一个套接字测试。我使用的是passport.socketio,所以当没有登录用户时,套接字不应该连接(因此套接字回调不会触发)。我想测试一下。

是否有一种方法来真正期待超时?

代码语言:javascript
复制
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. 
        });
    });
});

或者我应该用其他方法来处理这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-18 09:07:23

你基本上可以自己编程:

代码语言:javascript
复制
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模块缩短代码:

代码语言:javascript
复制
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()
    });
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23719916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档