了解 2023 年 Google I/O 大会上介绍的 Firebase 亮点。了解详情

在本地運行函數

Firebase CLI 包含一個 Cloud Functions 模擬器,它可以模擬以下函數類型:

  • HTTPS 函數
  • 可調用函數
  • 從 Firebase 身份驗證、實時數據庫、Cloud Firestore、Cloud Storage 和 Cloud Pub/Sub 觸發的後台函數。

您可以在本地運行函數以在部署到生產環境之前對其進行測試。

安裝 Firebase CLI

要使用 Cloud Functions 模擬器,請先安裝 Firebase CLI:

npm install -g firebase-tools

為了使用本地模擬器,您的 Cloud Functions 必須依賴於:

  • firebase-admin版本8.0.0或更高版本。
  • firebase-functions版本3.0.0或更高版本。

設置管理員憑據(可選)

如果您希望您的功能測試通過Firebase Admin SDK與 Google API 或其他 Firebase API 交互,您可能需要設置管理員憑據。

  • Cloud Firestore 和實時數據庫觸發器已經具有足夠的憑據,不需要額外設置。
  • 所有其他 API,包括身份驗證和 FCM 等 Firebase API 或 Cloud Translation 或 Cloud Speech 等 Google API,都需要本節中描述的設置步驟。無論您使用的是 Cloud Functions shell 還是firebase emulators:start這都適用。

為模擬功能設置管理員憑據:

  1. 打開 Google Cloud Console 的服務帳戶面板
  2. 確保選擇了App Engine 默認服務帳戶,並使用右側的選項菜單選擇Create key
  3. 出現提示時,選擇JSON作為密鑰類型,然後單擊創建
  4. 將您的 Google 默認憑據設置為指向下載的密鑰:

    Unix

    export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json"
    firebase emulators:start
    

    視窗

    set GOOGLE_APPLICATION_CREDENTIALS=path\to\key.json
    firebase emulators:start
    

完成這些步驟後,您的功能測試可以使用Admin SDK訪問 Firebase 和 Google API。例如,在測試身份驗證觸發器時,模擬函數可以調用admin.auth().getUserByEmail(email)

設置功能配置(可選)

如果您使用的是自定義函數配置變量,請首先運行命令以在本地環境中獲取您的自定義配置(在functions目錄中運行):

firebase functions:config:get > .runtimeconfig.json
# If using Windows PowerShell, replace the above with:
# firebase functions:config:get | ac .runtimeconfig.json

運行模擬器套件

要運行 Cloud Functions 模擬器,請使用emulators:start命令:

firebase emulators:start

emulators:start命令將根據您在本地項目中使用firebase init初始化的產品啟動 Cloud Functions、Cloud Firestore、Realtime Database 和 Firebase Hosting 的模擬器。如果要啟動特定的模擬器,請使用--only標誌:

firebase emulators:start --only functions

如果要在模擬器啟動後運行測試套件或測試腳本,請使用emulators:exec命令:

firebase emulators:exec "./my-test.sh"

檢測您的應用以與模擬器對話

要檢測您的應用以與模擬器交互,您可能需要進行一些額外的配置。

檢測您的應用程序以獲取可調用函數

如果您的原型和測試活動涉及可調用的後端函數,請像這樣配置與 Cloud Functions for Firebase 模擬器的交互:

Kotlin+KTX
// 10.0.2.2 is the special IP address to connect to the 'localhost' of
// the host computer from an Android emulator.
val functions = Firebase.functions
functions.useEmulator("10.0.2.2", 5001)
Java
// 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);
迅速
Functions.functions().useEmulator(withHost: "localhost", port: 5001)

Web modular API

import { getApp } from "firebase/app";
import { getFunctions, connectFunctionsEmulator } from "firebase/functions";

const functions = getFunctions(getApp());
connectFunctionsEmulator(functions, "localhost", 5001);

Web namespaced API

firebase.functions().useEmulator("localhost", 5001);

檢測您的應用程序以進行 HTTPS 功能模擬

您代碼中的每個 HTTPS 函數都將使用以下 URL 格式從本地模擬器提供服務:

http:// $HOST : $PORT / $PROJECT / $REGION / $NAME

例如,將在以下位置提供具有默認主機端口和區域的簡單helloWorld函數:

https://localhost:5001/ $PROJECT /us-central1/helloWorld

檢測您的應用程序以進行後台觸發的功能模擬

Cloud Functions 模擬器支持來自以下來源的後台觸發函數:

  • 實時數據庫模擬器
  • Cloud Firestore 模擬器
  • 身份驗證模擬器
  • 發布/訂閱模擬器

要觸發後台事件,請使用 Emulator Suite UI 修改後端資源,或者使用適用於您平台的 SDK 將您的應用程序或測試代碼連接到模擬器。

擴展程序發出的自定義事件的測試處理程序

對於您實現的用於使用 Cloud Functions v2 處理 Firebase Extensions 自定義事件的函數,Cloud Functions 模擬器與 Eventarc 模擬器配對以支持Eventarc 觸發器

要為發出事件的擴展測試自定義事件處理程序,您必須安裝 Cloud Functions 和 Eventarc 模擬器。

如果 Eventarc 模擬器正在運行,Cloud Functions 運行時會將EVENTARC_EMULATOR環境變量設置為當前進程中的localhost:9299 。設置EVENTARC_EMULATOR環境變量後,Firebase Admin SDK 會自動連接到 Eventarc 模擬器。您可以按照Configure Local Emulator Suite中的討論修改默認端口。

正確配置環境變量後,Firebase Admin SDK 會自動將事件發送到 Eventarc 模擬器。反過來,Eventarc 模擬器會回調 Cloud Functions 模擬器以觸發任何已註冊的處理程序。

您可以在 Emulator Suite UI 中查看 Functions 日誌,了解有關處理程序執行的詳細信息。

與其他服務的交互

模擬器套件包括多個模擬器,可以測試跨產品交互。

雲端 Firestore

如果您有使用 Firebase Admin SDK 寫入 Cloud Firestore 的函數,這些寫入將發送到正在運行的 Cloud Firestore 模擬器。如果這些寫入觸發了更多功能,它們將在 Cloud Functions 模擬器中運行。

雲儲存

如果您有使用 Firebase Admin SDK(版本 9.7.0 或更高版本)寫入 Cloud Storage 的函數,這些寫入將發送到正在運行的 Cloud Storage 模擬器。如果這些寫入觸發了更多功能,它們將在 Cloud Functions 模擬器中運行。

Firebase 身份驗證

如果您有使用 Firebase Admin SDK(版本 9.3.0 或更高版本)寫入 Firebase Authentication 的函數,這些寫入將被發送到 Auth 模擬器(如果它正在運行)。如果這些寫入觸發了更多功能,它們將在 Cloud Functions 模擬器中運行。

火力地堡託管

如果您使用 Cloud Functions為 Firebase Hosting 生成動態內容firebase emulators:start會使用您的本地 HTTP 函數作為託管代理。

記錄

模擬器將日誌從您的函數流式傳輸到它們運行的終端窗口。它顯示函數內console.log()console.info()console.error()console.warn()語句的所有輸出。

下一步

有關使用 Firebase 模擬器套件的完整示例,請參閱測試快速入門示例