【问题标题】:Mocking admin.firestore().collection().get()模拟 admin.firestore().collection().get()
【发布时间】:2018-09-22 19:59:26
【问题描述】:

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

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

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

我试图模拟它看起来像这样:

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

但它不起作用。

我已经创建了一个 repo(官方函数 repo 的克隆),如果有帮助,请给出整个示例 here

【问题讨论】:

  • 在您的代码示例中,firestoreadmin 的函数,但您正试图将其定义为属性。在您的模拟 admin 对象上定义一个函数或模拟,它返回您的模拟 firestore
  • 嗨@DauleDK,您介意在下面发布您的解决方案作为答案吗?提前致谢!

标签: javascript firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

在 Mark 的帮助下,我完成了这项工作:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-01
    • 2018-09-17
    • 2019-05-28
    • 2018-06-16
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多