testing摩卡预期的超时时间

我有一个Faye发布/订阅服务器,只有当消息已经发送给其他订阅者时,才向新订阅者发送消息。 我正在使用摩卡进行unit testing。

这个testing的目的是确认基本function:

it('should send a special message to new subscribers', function(done){ testerPubSub.setLastJsonContent('some arbitrary content'); var subscription = client.subscribe("/info", function(message) { assert.equal(true, message.text.indexOf('some arbitrary content') !== -1, 'new subscriber message not sent'); done(); }); subscription.cancel(); }); 

但是现在我想testing一下没有消息发送给以前的用户的情况。 这个testing会是这样的:

  it('should not send a special message to new subscribers if no data has been retrieved', function(done){ testerPubSub.setLastJsonContent(null); var messageReceived = false; var subscription = client.subscribe("/sales", function(message) { messageReceived = true; }); ////need to magically wait 2 seconds here for the subscription callback to timeout assert.equal(false, messageRecieved, 'new subscriber message received'); subscription.cancel; done(); }); 

当然,神奇的睡眠function是有问题的。 有没有更好的方法来做这种“我希望callback永远不会被解雇”的testing?

谢谢,迈克

我想到了一个可能的解决scheme,但是我觉得这个解决scheme有点太依赖于时机了:

  it('should not send a special message to new subscribers if no data has been retrieved', function(done){ testerPubSub.setLastJsonContent(null); var messageReceived = false; var subscription = client.subscribe("/sales", function(message) { assert.fail(message.text, '', 'Should not get message', '='); }); setTimeout(function(){ done(); }, 1500); subscription.cancel; }); 

这个testing通过了,但1500毫秒的停顿似乎是相当随意的。 我真的说:“在我认为它会超时之前,跳进来说好。