【发布时间】:2021-12-31 17:52:36
【问题描述】:
对于 Firestore 云函数 TypeScript 单元测试,我想模拟 doc().id,但 不是 doc('path')。我该怎么做?
admin.firestore().collection('posts').doc().id // I only want to mock this one
admin.firestore().collection('posts').doc('1')
我尝试在 sinon 中执行以下操作。但它在sinon/proxy-invoke.js:50:47 处陷入无限循环:
const collection = admin.firestore().collection('posts');
sinon.stub(collection. 'doc').callsFake(path =>
path === undefined ? mock : collection.doc(path)
);
sinon.stub(admin.firestore(), 'collection')
.callThrough()
.withArgs('posts')
.returns(collection)
我还尝试了以下方法。但是doc(documentPath: string) 方法似乎也被淘汰了:
sinon.stub(collection, 'doc')
//@ts-ignore
.withArgs()
.returns(mock)
如果有解决方法,我愿意使用其他模拟库。
【问题讨论】:
标签: typescript google-cloud-firestore google-cloud-functions sinon