本文介绍了模拟admin.firestore().collection().get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关此[问题] [1],我正在尝试进行单元测试时模拟firestore.

Related to this [question][1], I am trying to mock firestore when doing unit tests.

我要模拟的代码如下:

const firestore = admin.firestore();
const users = await firestore.collection('users').get();

我尝试模拟它的方法如下:

And my attempt to mock it looks like this:

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

但是它不起作用.

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

I have created a repo (a clone of the official functions repo), to give the entire example here if it helps.

推荐答案

在Mark的帮助下,我的工作正常:

With the help from Mark, I got this working:

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

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

It looks crazy - so if anyone knows a better solution, let me know!

这篇关于模拟admin.firestore().collection().get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:03