【发布时间】:2018-05-31 19:05:28
【问题描述】:
我有一个初始化方法调用另一个返回承诺的方法,例如:
initStuffAfterLoad() {
const _this = this;
const theInterval = window.setInterval(function() {
if (thing) {
window.clearInterval(theInterval);
_this.getBanana()
.then(response => {
_this.getApple(response, _this);
});
}
}, 100);
}
并且我需要测试是否调用了 getBanana (jest/sinon)。到目前为止,我有:
test('init function calls getBanana', () => {
let thing = true
const getBananaSpy = sinon.spy();
sinon.stub(TheClass.prototype, 'getBanana').callsFake(getBananaSpy).resolves();
jest.useFakeTimers();
TheClass.prototype.initStuffAfterLoad();
jest.runOnlylPendingTimers();
expect(getBananaSpy.called).toBeTruthy();
TheClass.prototype.getBanana.restore();
});
但是它仍然在断言中收到false。我认为我没有正确处理 Promise 部分 - 最佳实践方法是什么?
【问题讨论】: