英文:
Testing firebase cloud functions - validate database changes
问题
基于云函数单元测试的文档,我理解如何有效地测试相对简单的云函数,通过断言响应是否符合我的期望。然而,我有一些相对复杂的云函数,在我的数据库中某种数据类型发生更改时,会有多个私有方法检索其他类型的数据,对这些数据进行计算,然后将多个数据库更新调用放入一个承诺数组中,并调用 Promise.all(my_array)
来完成我的云函数执行。
这导致响应是一个可变长度和组成的数组,非常难以验证。
相反,我希望能够按照以下方式按顺序测试云函数:
- 设置初始数据库
- 触发云函数
- 检查更新后的数据库是否符合我的期望
是否可以使用 firebase-functions-test
库来做类似的事情?我该如何做?我理解上述情景更像是一个集成测试,但我仍然想知道该库是否能够处理它。
英文:
Based on the documentation for unit testing Cloud Functions, I understand how I could effectively test rather simple Cloud Functions by asserting a response to be what I would expect it. However, I have a few somewhat complex Cloud Functions, where on change of one type of data in my database, a number of private methods retrieve a few other types of data, make calculations on those, I then put a number of database update calls into an array of promises and call Promise.all(my_array)
on it to complete my Cloud Function execution.
This results in the response being an array of variable length and composition, which is very difficult to validate.
Instead, I would prefer to be able to test Cloud Functions in a sequence something like below:
- Set an initial database
- Trigger a Could Function
- Check the updated database if all is as I expect
Is it possible to do something like this with the firebase-functions-test
library? How do I do it? I understand the above scenario is more of an integration test, still I'd like to know if said library would be able to handle it.
答案1
得分: 0
你所描述的一切都可以通过 Firebase 模拟器套件来实现,这是更合适的选择,该套件包括以下内容:
Firebase 本地模拟器套件由各个服务模拟器组成,能够准确模拟 Firebase 服务的行为。这意味着你可以直接将你的应用连接到这些模拟器,以进行集成测试或质量保证,而无需触及生产数据。
例如,你可以将你的应用连接到 Firestore 模拟器,以安全地在测试中读取和写入文档。这些写操作可能会触发 Cloud Functions 模拟器中的函数。但是,当模拟器不可用或未配置时,你的应用仍将继续与生产 Firebase 服务通信。
了解更多信息,请参阅文档。
英文:
What you're describing should all be possible with (and is a better fit for) the Firebase emulator suite, which is:
> The Firebase Local Emulator Suite consists of individual service emulators built to accurately mimic the behavior of Firebase services. This means you can connect your app directly to these emulators to perform integration testing or QA without touching production data.
>
> For example, you could connect your app to the Firestore emulator to safely read and write documents in testing. These writes may trigger functions in the Cloud Functions emulator. However your app will still continue to communicate with production Firebase services when emulators are not available or configured.
For more on this, see the documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论