在本機執行函式

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,包括 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
    

完成上述步驟後,函式測試即可透過 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);
Swift
Functions.functions().useFunctionsEmulator(origin: "http://127.0.0.1:5001")

網頁模組 API

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

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

網路命名空間 API

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,將應用程式或測試程式碼連結到模擬器。

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

針對透過 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 中查看函式記錄,進一步瞭解處理常式執行作業。

與其他服務的互動

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

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 驗證,系統會將這些寫入內容傳送至驗證模擬器 (如果正在執行)。如果這類寫入作業觸發了後續函式,就會在 Cloud Functions 模擬器中執行。

Firebase 託管

如果您使用 Cloud Functions 來產生 Firebase 託管的動態內容firebase emulators:start 會使用本機 HTTP 函式做為託管的 Proxy。

Logging

模擬器會將函式記錄串流至執行函式的終端機視窗。會顯示在函式中 console.log()console.info()console.error()console.warn() 陳述式的所有輸出內容。

後續步驟

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