【问题标题】:Is it possible to test a Firebase trigger locally?是否可以在本地测试 Firebase 触发器?
【发布时间】:2018-03-18 18:55:01
【问题描述】:

firebase-functions 与Node 和exports.foo = functions.database.ref('/object').onWrite(event => {}); 一起使用。

我可以部署到云端并进行很好的测试......我可以使用firebase serve --only functions轻松地在本地测试http类型的函数。

但是,我看不到在本地测试触发器的方法。可能吗?怎么样?

【问题讨论】:

    标签: node.js firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    这现在是可能的。我终于找到了这个明确的媒体帖子,解释了它是如何做到的:https://medium.com/@moki298/test-your-firebase-cloud-functions-locally-using-cloud-functions-shell-32c821f8a5ce

    要在本地运行触发器,请使用“functions shell”工具:

    1 - 我的 firestore 代码(中等帖子使用实时数据库):

    exports.testTrigger = functions.firestore
    .document('messages/{messageId}')
    .onCreate((snapshot, context) => {
        let message = snapshot.data()
        return snapshot.ref.set({
            ...message,
            status : 'sent' 
        })
    })
    

    2 - 我在终端中运行 firebase shell 函数

    firebase functions:shell
    

    3 - 我用参数中的数据调用我的“testTrigger”云函数触发器

    testTrigger({text:"hello"})
    

    4- 我的 firestore 数据库有一条由我的触发器更新的新“消息”

    【讨论】:

      【解决方案2】:

      最近,Firebase 团队发布了对 Firebase CLI 的更新,增加了在本地调用其他类型触发器的功能。它记录在here。确保您已将 CLI 更新到最新版本以获取此新功能:npm install -g firebase-tools@latest

      【讨论】:

      • 不是一回事 - 在本地调用触发器 != 从部署的函数中获取相同的数据。我想看到的是本地运行的服务器从云数据库接收事件并调用该函数。我现在要做的是部署和查看日志,这很痛苦。
      • 您所描述的内容今天不可用,除非您想编写自己的代码来完成它。
      【解决方案3】:

      Doug 在评论中回答了这个问题。虽然在某些情况下调用触发器可能很有用(感谢 Doug),但它与部署的触发器不同,后者还不能完成。

      所以我会坚持远程部署并查看远程日志......很好的 'ole Marco Polo 调试。

      【讨论】:

      • 现在可以吗?我发现文档在这个问题上有点模棱两可,并且在模拟数据库触发函数时很难获得更新
      【解决方案4】:

      自 2020 年 5 月 21 日起,现在可以在本地测试触发器了!

      查看发布博文https://firebase.googleblog.com/2020/05/local-firebase-emulator-ui.html

      还可以看看发行说明https://firebase.google.com/support/releases#may_21_2020

      请注意,8.4 需要节点 10,如问题 Update of Firebase CLI to 8.4.0 gives errors about "Unsupported engine" saying '{"node":">=10"}' 中所述

      【讨论】:

        猜你喜欢
        • 2018-04-16
        • 2018-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多