如何存储与sinon的asynchronous数据库调用

我想unit testing这个类的getAll方法:

 module.exports = class BookController { static async getAll() { return await Book.query() .eager('reviews'); } }; 

本书是一个异议模型。

我想testing它是我想用我自己的数据伪造Book.query().eager()的响应,因为我永远不能validation数据库中的内容。 为了成为一个真正的unit testing,我应该只是testingBook.query()方法被调用? 或者我应该testing返回的数据,因为这是getAll()方法的契约? 我真的不确定我应该如何把这一点弄糟。

用Sinon> 2.x,你可以在你的存根上调用.resolves() 。 我会做类似的事情

 var stub = sinon.stub(BookController, 'getAll'); stub.resolves({response:"ok"}); var bc = new BookController(); bc.getAll.then(function(data){ expect(data.response).to.equal("ok"); done(); },function(err){ done("should NEVER get here"); });