【发布时间】:2016-05-14 22:32:48
【问题描述】:
我试图弄清楚当异步发出的事件已经发出时如何使用 sinon.js 进行测试?
到目前为止,我正在做的是设置一个超时时间,我知道该事件将被粗暴地发出,但这很丑陋,可能会增加测试运行的总时间,我不想这样做:
it('check that event was called', function(done) {
...
var spy = sinon.spy();
var cbSpy = sinon.spy();
obj.on('event', spy);
obj.func(cbSpy); // emits event 'event' asynchronously and calls cbSpy after it was emitted
setTimeout(function() {
sinon.assert.calledOnce(spy, 'event "event" should be emitted once');
sinon.assert.calledOnce(cbSpy, 'func() callback should be called once'); // won't work since the callback will be called only after the event has been emitted and all event listeners finished
}, 1000);
});
【问题讨论】:
标签: javascript node.js unit-testing mocha.js sinon