我有一个 Faye 发布/订阅服务器,仅当消息已发送给其他订阅者时才向新订阅者发送消息。我正在使用 mocha 进行单元测试。
此测试用于确认基本功能:
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();
});
但是我现在想测试没有消息发送给以前的订阅者的情况。该测试将类似于:
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();
});
当然,神奇的睡眠功能是有问题的。有没有更好的方法来做这种“我希望回调永远不会被解雇”的测试?
谢谢,迈克