sinon-as-promise不能正确返回?

问题描述:

我想知道为什么下面的两个方法返回不同的东西。我希望两者都能够以字符串值“'返回已解决的承诺。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] } 
*/ 

我必须阅读文档错了吗?

相关的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