模拟admin.firestore()。collection()。get()

Dau*_*eDK 5 javascript firebase google-cloud-functions google-cloud-firestore

与此[question] [1]相关,我正在尝试进行单元测试时模拟firestore。

我尝试模拟的代码如下所示:

const firestore = admin.firestore();
const users = await firestore.collection('users').get();
Run Code Online (Sandbox Code Playgroud)

而我尝试模拟它的过程如下:

const firestoreStub = sinon.stub();
Object.defineProperty(admin, 'firestore', {
  get: () => {
    return {
      collection: (path) => Promise.resolve({mocka: 'user'})
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

但是,它不起作用。

我创建了一个仓库(官方功能仓库的克隆),如果有帮助,请在此处给出整个示例。

小智 5

在马克的帮助下,我得到了这个工作:

sinon.stub(admin, 'firestore')
        .get(() => {
            return function() {
                return {
                    collection: (path) => {
                        return {
                            get: () => [{user: 'mock-user-1'}, {user: 'mock-user-2'}]
                        }
                    }
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

它看起来很疯狂 - 所以如果有人知道更好的解决方案,请告诉我!