0
時功能不受柴間諜承認我有一個返回藍鳥承諾兩個異步功能:興農存根斷言函數調用
Async1: function() {
return new Promise(function(resolve, reject) {
execute(query)
.then(function(resp) {
resolve(resp);
})
.catch(function(err) {
reject(err);
});
});
}
Async2: function() {
return new Promise(function(resolve, reject) {
execute(query2)
.then(function(resp) {
resolve(resp);
})
.catch(function(err) {
reject(err);
});
});
}
我還有一個模塊調用這些方法,像這樣:
module.exports = Foo {
Bar: require(./'Bar');
caller: function() {
this.Bar.Async1()
.then(function(resp) {
this.Bar.Async2()
.then(function(resp) {
// do something
}.bind(this))
}.bind(this))
}
}
在我的測試案例中,我想檢查Bar.Async2是否被調用,並且我有以下測試用例失敗:
it('should call Foo.Bar.Async2', function(done) {
var spy;
sinon.stub(Foo.Bar, 'Async1').returns(
new Promise(function(resolve) {
resolve();
})
);
sinon.stub(Foo.Bar, 'Async2').returns(
new Promise(function(resolve) {
resolve();
})
);
spy = chai.spy.on(Foo.Bar, 'Async2');
Foo.caller();
expect(spy).to.be.called();
done();
});
我從控制檯日誌中知道Async2確實被調用,所以我想知道爲什麼間諜不會選擇它?