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,包括 Firebase API(如身份驗證和 FCM)或 Google API(如 Cloud Translation 或 Cloud Speech),都需要執行本節中描述的設置步驟。無論您使用的是 Cloud Functions shell 還是
firebase emulators:start
這都適用。
要為模擬功能設置管理員憑據:
- 打開 Google Cloud Console 的服務帳戶窗格。
- 確保選擇App Engine 默認服務帳戶,然後使用右側的選項菜單選擇Create key 。
- 出現提示時,選擇JSON作為密鑰類型,然後單擊Create 。
將您的 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、實時數據庫和 Firebase 託管的模擬器。如果您想啟動特定的模擬器,請使用--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().useFunctionsEmulator(origin: "http://127.0.0.1:5001")
Web modular API
import { getApp } from "firebase/app"; import { getFunctions, connectFunctionsEmulator } from "firebase/functions"; const functions = getFunctions(getApp()); connectFunctionsEmulator(functions, "127.0.0.1", 5001);
Web namespaced API
firebase.functions().useEmulator("127.0.0.1", 5001);
為您的應用程序檢測 HTTPS 功能模擬
代碼中的每個 HTTPS 函數都將使用以下 URL 格式從本地模擬器提供服務:
http:// $HOST : $PORT / $PROJECT / $REGION / $NAME
例如,具有默認主機端口和區域的簡單helloWorld
函數將在以下位置提供服務:
https://localhost:5001/ $PROJECT /us-central1/helloWorld
為您的應用程序配備後台觸發功能模擬
Cloud Functions 模擬器支持以下來源的後台觸發函數:
- 實時數據庫模擬器
- Cloud Firestore 模擬器
- 認證模擬器
- 發布/訂閱模擬器
要觸發後台事件,請使用模擬器套件 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 模擬器。您可以按照配置本地模擬器套件中的討論修改默認端口。
正確配置環境變量後,Firebase Admin SDK 會自動將事件發送到 Eventarc 模擬器。反過來,Eventarc 模擬器會回調 Cloud Functions 模擬器以觸發任何已註冊的處理程序。
您可以檢查模擬器套件 UI 中的函數日誌,了解有關處理程序執行的詳細信息。
與其他服務的交互
模擬器套件包括多個模擬器,可以測試跨產品交互。
雲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 身份驗證的函數,則這些寫入將被發送到正在運行的 Auth 模擬器。如果這些寫入觸發更多函數,它們將在 Cloud Functions 模擬器中運行。
Firebase 託管
如果您使用 Cloud Functions為 Firebase Hosting 生成動態內容, firebase emulators:start
使用您的本地 HTTP 函數作為託管代理。
記錄
模擬器將日誌從您的函數傳輸到它們運行的終端窗口。它顯示函數內console.log()
、 console.info()
、 console.error()
和console.warn()
語句的所有輸出。
下一步
有關使用 Firebase 模擬器套件的完整示例,請參閱測試快速入門示例。