尝试使用Sinon存根链接的knex查询 . 查询如下所示

const result = await TableModel
  .query()
  .whereIn('id', idList)
  .whereIn('value', valueList);

通常我使用一个我创建的辅助函数,它返回一个模型实例,每个方法都存根,返回 this ,就像这样

for (const method of ['query', 'whereIn']) {
  TableModel[method] = sandbox.stub().returnsThis();
}

然后在测试中对实例进行存根以解决必要的测试用例

TableModel.whereIn = sandbox.stub().resolves({ object: 'stuff' });

但是,当链接相同的方法时,这不起作用我从mocha / chai / sinon读取错误

TypeError: TableModel.query(...).whereIn(...).whereIn is not a function

寻求有关如何在测试中存根和解析方法的帮助 .