我在mocha的then语句中将使用几次代码块,因此将其转换为函数。但是,我还需要done()
在该函数内调用,并且该函数超出范围,从而导致错误Uncaught ReferenceError:done未定义。这是一个代码片段:
var collectionChecker = function(results) {
expect(Array.isArray(results), 'Did not return collection');
expect(results.length === numAttr, 'Returned wrong number of models');
done();
};
test('returns a collection with correct number of models', function(done) {
attrs.getAttributeTemplates().then(collectionChecker);
});
如何传递done()
给我的职能?
我找到了解决方法,方法是将另一个.then链接起来并在那里进行调用,但这似乎是一种丑陋的方法。
您想得太过分了-摩卡咖啡支持诺言,您可以返回诺言,如果诺言得以实现,则测试将通过(如果期望抛出则失败):
var collectionChecker = function(results) {
expect(Array.isArray(results), 'Did not return collection');
expect(results.length === numAttr, 'Returned wrong number of models');
};
// just add a return, no `done` here or anywhere
test('returns a collection with correct number of models', function() {
return attrs.getAttributeTemplates().then(collectionChecker);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句