我必须测试一个使用html2canvas库的ember应用程序,但是测试失败了,我相信它是因为它的承诺。我已经像这样包装了异步部分:
/**
* This method render DOM object (webpage) and generated canvas object using html2canvas library.
* After getting canvas object it set image property.
*
* @method 'captureScreen'
* @param {DOM}
* @return {Ember.RSVP.Promise}
*/
captureScreen: function (dom) {
return new Ember.RSVP.Promise((resolve) => {
html2canvas(dom).then((canvas) => {
resolve(canvas);
});
});
}
所以现在我试图像这样将html2canvas lib本身存根:
stubHtml2Canvas = function () {
_stubHtml2Canvas = sinon.stub(html2canvas).returns(new Ember.RSVP.Promise(function (resolve) {
resolve(document.createElement("CANVAS"));
}));
}
但这会引发以下错误:
beforeEach失败时应该在单击洞察力捕获按钮时显示叠加:尝试将未定义属性未定义为函数
我也试过
sinon.createStubInstance(html2canvas).returns(new Ember.RSVP.Promise(function (resolve) {
resolve(document.createElement("CANVAS"));
}));
但那也没有用。
我可以删除" captureScreen"方法本身并使事情有效但我宁愿学习替代选项。