2016-11-16 57 views
0

我想知道爲什麼下面的兩個方法返回不同的東西。我希望兩者都能夠以字符串值「'返回已解決的承諾。sinon-as-promise不能正確返回?

使用sinon模塊:

sinon.stub(db, 'query').returns(Promise.resolve('<VALUE>')); 
console.log(db.query()); 
// echos: Promise { '<VALUE>' } 

,然後使用sinon-as-promised模塊:

sinon.stub(db, 'query').resolves('<VALUE>'); 
console.log(db.query()); 
/* echos: 
    { then: [Function: then], 
     catch: [Function], 
     finally: [Function] } 
*/ 

我必須閱讀文檔錯了嗎?

回答

0

相關的documentationstub.resolves(value)指出:

調用時,存根將返回「thenable」對象,這將 返回承諾爲所提供的價值。

要記錄「thenable」對象到控制檯,而不是這種「thenable」對象返回的Promise。你可以這樣做記錄值:

var promise = sinon.stub(db, 'query').resolves('<VALUE>').then(function({ 
}); 

console.log(promise); 

sinon.stub(db, 'query').resolves('<VALUE>').then(function(value){ 
    console.log(value); // <VALUE> 
}); 

您也可以通過這樣的記錄返回Promise