在将应用连接到 Cloud Functions 模拟器之前,请务必了解 Firebase Local Emulator Suite 整体工作流并安装和配置此模拟器套件。
对您的应用进行插桩处理以与模拟器通信
针对 Callable 函数对您的应用进行插桩处理
如果您的原型设计和测试活动涉及 Callable 后端函数,请按如下方式配置与 Cloud Functions for Firebase 模拟器的交互:
Android
// 10.0.2.2 is the special IP address to connect to the 'localhost' of // the host computer from an Android emulator. FirebaseFunctions functions = FirebaseFunctions.getInstance(); functions.useEmulator("10.0.2.2.", 5001);
iOS - Swift
Functions.functions().useFunctionsEmulator(origin: "http://localhost:5001")
Web
firebase.functions().useEmulator("localhost", 5001);
针对 HTTPS 函数模拟对您的应用进行插桩处理
代码中的每个 HTTPS 函数都将从本地模拟器按以下网址格式提供:
http://$HOST:$PORT/$PROJECT/$REGION/$NAME
例如,采用默认主机端口和区域的简单 helloWorld
函数将在以下位置提供:
https://localhost:5001/$PROJECT/us-central1/helloWorld
针对后台触发的函数模拟对您的应用进行插桩处理
Cloud Functions 模拟器支持以下来源的后台触发函数:
- Realtime Database 模拟器
- Cloud Firestore 模拟器
- Authentication 模拟器
- Pub/Sub 模拟器
要触发后台事件,请使用适用于您的平台的 SDK 将应用或测试代码连接到模拟器。
后续步骤
- 如需查看一组精选视频和详细的操作方法示例,请点击 Firebase 模拟器培训播放列表。
- 如需详细了解 Cloud Functions for Firebase 模拟器,请参阅在本地运行函数。