在本機執行函式

Firebase CLI 提供可模擬 下列函式類型:

  • 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 以上版本。

設定管理員憑證 (選用)

如果您希望函式測試與 Google API 或其他 Firebase 互動 API (透過 Firebase Admin SDK) 可能需要設定 管理員憑證

  • Cloud Firestore 和即時資料庫觸發條件已具備足夠的憑證,不需要進行其他設定
  • 至於其他 API,包括 Authentication API (例如 FCM) 或 Google API (例如 Cloud Translation 或 Cloud Speech) 時,都必須完成本節所述的設定步驟。無論是使用 Cloud Functions 殼層還是 firebase emulators:start,都適用。

如何為模擬函式設定管理員憑證:

  1. 開啟 服務帳戶窗格 就能存取 Google Cloud 控制台
  2. 確認已選取 [App Engine default service account] (App Engine 預設服務帳戶),並使用 透過右側的選項選單選取「Create key」(建立金鑰)
  3. 系統提示時,請選取「JSON」做為金鑰類型,然後按一下「建立」
  4. 將 Google 預設憑證設為指向已下載的金鑰:

    Unix

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

    Windows

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

完成這些步驟後,函式測試就能存取 Firebase 和 使用 Admin SDK 的 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 指令會啟動 Cloud Functions 的模擬器。 以產品為基礎的 Cloud Firestore、即時資料庫和 Firebase 託管 您已在本機專案中使用 firebase init 初始化。如果您希望 如要啟動特定模擬器,請使用 --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);
Swift
Functions.functions().useFunctionsEmulator(origin: "http://127.0.0.1:5001")

Web

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

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

Web

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

檢測應用程式的 HTTPS 函式模擬結果

程式碼中的每個 HTTPS 函式都會使用以下網址格式,透過本機模擬器提供:

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

例如,一個含有預設主機通訊埠和區域的簡易 helloWorld 函式會在下列位置提供:

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

檢測應用程式以進行背景觸發函式模擬

Cloud Functions 模擬器支援下列來源的背景觸發函式:

  • 即時資料庫模擬器
  • Cloud Firestore 模擬器
  • 驗證模擬器
  • Pub/Sub 模擬器

如要觸發背景事件,請使用 模擬器套件 UI,或將應用程式或測試程式碼連線至模擬器。 導入適用於您平台的 SDK

測試擴充功能產生的自訂事件專用處理常式

針對為了處理 Firebase Extensions 自訂事件而導入的函式 與 Cloud Functions v2,Cloud Functions 模擬器會與 支援的 Eventarc 模擬器 Eventarc 觸發條件

如要針對發出事件的擴充功能測試自訂事件處理常式,您必須安裝 包括 Cloud Functions 和 Eventarc 模擬器

Cloud Functions 執行階段會設定 EVENTARC_EMULATOR 環境 設為 localhost:9299 時,如果 Eventarc 模擬器 正在運作。Firebase Admin SDK 會自動連線至 Eventarc 在已設定 EVENTARC_EMULATOR 環境變數的情況下模擬器。你可以 修改預設通訊埠,如「設定本機模擬器套件」一節所述。

正確設定環境變數時,Firebase Admin SDK 自動將事件傳送至 Eventarc 模擬器。反之,Eventarc 模擬器會傳回 Cloud Functions 模擬器呼叫,以觸發任何 註冊處理常式

您可以在模擬器套件 UI 中查看函式記錄,進一步瞭解 處理常式執行作業。

與其他服務的互動

模擬器套件包含多個模擬器 測試跨產品互動情形

Cloud Firestore

如果您有函式會使用 Firebase Admin SDK 寫入 Cloud Firestore,這些寫入內容會傳送至 Cloud Firestore 模擬器 以及是否正在運作如果這些寫入動作觸發了後續函式 只會在 Cloud Functions 模擬器中執行。

Cloud Storage

如果您的函式使用 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 託管firebase emulators:start 使用您的本機 HTTP 函式做為代管的 Proxy。

記錄

模擬器會將函式記錄串流至終端機視窗 run.並顯示 console.log()console.info()、 函式中的 console.error()console.warn() 陳述式。

後續步驟

如需 Firebase 模擬器套件的完整使用範例,請參閱 測試快速入門導覽課程範例