是否可以在本地测试Firebase触发器? [英] Is it possible to test a Firebase trigger locally?
问题描述
将firebase-functions
与Node和exports.foo = functions.database.ref('/object').onWrite(event => {});
一起使用.
Using firebase-functions
with Node and exports.foo = functions.database.ref('/object').onWrite(event => {});
.
我可以部署到云中并进行很好的测试...,并且可以使用firebase serve --only functions
在本地轻松地测试http类型的功能.
I can deploy to the cloud and test just fine ... and I can easily test http-type functions locally using firebase serve --only functions
.
但是,我没有一种在本地测试触发的方法.是否有可能?怎么样?
However, I don't see a way to test triggers locally. Is it possible? How?
推荐答案
现在可以做到这一点.我终于找到这篇清楚的中等文章,解释了它的用法:
This in now possible. I finally find this clear medium post which explain how do it : https://medium.com/@moki298/test-your-firebase-cloud-functions-locally-using-cloud-functions-shell-32c821f8a5ce
要在本地运行触发器,请使用功能外壳"工具:
To run a trigger locally, use the "functions shell" tool :
1-我的Firestore代码(中级使用RealTime数据库):
1 - My code for firestore (the medium post use RealTime Database) :
exports.testTrigger = functions.firestore
.document('messages/{messageId}')
.onCreate((snapshot, context) => {
let message = snapshot.data()
return snapshot.ref.set({
...message,
status : 'sent'
})
})
2-我在终端中运行firebase shell函数
2 - I run the firebase shell functions in the terminal
firebase functions:shell
3-我用参数中的数据调用"testTrigger"云函数触发器
3 - I call my "testTrigger" cloud function trigger with data in parameter
testTrigger({text:"hello"})
4-我的Firestore数据库中有一个由我的触发器更新的新消息"
4- My firestore database has a new "message" updated by my trigger
这篇关于是否可以在本地测试Firebase触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!