【问题标题】:cloud function callable [indirect call] test.wrapped(function) vs [direct call] function.run()云函数可调用 [间接调用] test.wrapped(function) vs [直接调用] function.run()
【发布时间】:2021-12-27 07:41:45
【问题描述】:

test.wrapped(function)function.run() 有什么区别?两者似乎都在本地测试运行器下运行良好。我使用了 mocha,但我认为它在 jest 下也会以相同的方式运行。

以下两种方法有什么区别?

1.直接调用:

.....
.....
.....
funcName.run({foo:"bar"}, { auth: { uid: "ABC" } });
.....
.....
.....

2。 [间接调用] 使用 wrap:

.....
.....
.....
const projectConfig = {
  projectId: 'my-project',
  databaseURL: 'https://my-project.firebaseio.com'
};
const test = require('firebase-functions-test')(projectConfig, './service-account-key.json');
wrapped = test.wrap(funcName);
expect(
        wrapped(
            { foo: "bar" },
            { auth: { uid: "ABC" } }
        )
).to.contain('foobar');
.....
.....
.....

我们是否需要使用 test.wrap() 来利用测试运行器的额外功能?这也可以通过 async await 来实现,但是上面提到的使用起来会很方便。例如。 eventually 来自 chai-as-promised 的承诺?

【问题讨论】:

  • 嗨@Mehul Pamale,如果您认为我的回答对您有所帮助,请考虑通过单击投票箭头下方左侧的复选标记并投票来接受它。非常感谢,谢谢!
  • 完成!谢谢您的回答。欣赏!

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


【解决方案1】:

直接调用和间接调用(使用 wrap)之间的区别在于,后一种使用包装函数的方法利用了 Firebase Test SDK for Cloud Functions 的功能 - “firebase-functions-test”。其中一些功能是 -

  1. 提供在数据参数中构建自定义数据或示例数据的方法。

  2. 为包装函数生成默认 eventContextOptions (2nd) 参数,以防此参数未显式传入。

您可以在 Cloud Functions public docs for unit testing & corresponding API reference for Firebase Test SDK 上阅读有关此主题的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多