在本機執行函式

Firebase CLI 包含 Cloud Functions 模擬器,可模擬下列函式類型:

  • HTTPS 函式
  • 可呼叫的函式
  • 工作佇列函式
  • Firebase AuthenticationRealtime DatabaseCloud FirestoreCloud Storage、支援的 Firebase 快訊和 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 FirestoreRealtime Database 觸發條件已具備足夠的憑證,需要額外設定。
  • 所有其他 API (包括 Firebase API,例如 AuthenticationFCM,或 Google API,例如 Cloud Translation 或 Cloud Speech) 都需要本節所述的設定步驟。無論您使用的是 Cloud Functions 殼層或 firebase emulators:start,都適用這項功能。

如要為模擬函式設定管理員憑證,請按照下列步驟操作:

  1. 開啟 Google Cloud 主控台的「Service Accounts」(服務帳戶) 窗格
  2. 確認已選取App Engine預設服務帳戶,然後使用右側的選項選單選取「建立金鑰」
  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。舉例來說,在測試 Authentication 觸發程序時,模擬函式可以呼叫 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 Firestore、即時資料庫和 Firebase Hosting 的模擬器。Cloud Functions如要啟動特定模擬器,請使用 --only 旗標:

firebase emulators:start --only functions

如要在模擬器啟動後執行測試套件或測試指令碼,請使用 emulators:exec 指令:

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

檢測應用程式,與模擬器通訊

如要設定應用程式與模擬器互動,您可能需要進行一些額外設定。

檢測應用程式的可呼叫函式

如果原型和測試活動涉及可呼叫的後端函式,請按照下列方式設定與 Cloud Functions for Firebase 模擬器的互動:

Kotlin
// 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().useEmulator(withHost: "localhost", port: 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_TASKS_EMULATOR_HOST 環境變數執行,Admin SDK 就會將已加入佇列的要求重新導向至模擬器。

請注意,實際工作環境中使用的調度系統比模擬器中實作的系統更複雜,因此您不應期望模擬行為能精確反映實際工作環境。模擬器中的參數會提供工作調度和重試速率的上限。

檢測應用程式,模擬背景觸發的函式

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

  • Realtime Database 模擬器
  • Cloud Firestore 模擬器
  • Authentication 模擬器
  • Pub/Sub 模擬器
  • Firebase 警告模擬器

如要觸發背景事件,請使用 Emulator Suite UI 修改後端資源,或使用適用於您平台的 SDK,將應用程式或測試程式碼連線至模擬器。

測試擴充功能發出的自訂事件處理常式

如果您導入函式來處理 Cloud Functions 自訂事件 (使用 Cloud Functions 第 2 版),Cloud Functions 模擬器會與 Eventarc 模擬器配對,支援 Eventarc 觸發條件Firebase Extensions

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

如果 Eventarc 模擬器正在執行,Cloud Functions 執行階段會在目前程序中將 EVENTARC_EMULATOR 環境變數設為 localhost:9299。設定 EVENTARC_EMULATOR 環境變數後,Firebase Admin SDKs 會自動連線至 Eventarc 模擬器。如要修改預設通訊埠,請參閱「設定 Local Emulator Suite」一節。

環境變數設定完成後,Firebase Admin SDK 會自動將事件傳送至 Eventarc 模擬器。接著,Eventarc 模擬器會回呼 Cloud Functions 模擬器,觸發所有已註冊的處理常式。

如要查看處理常式執行作業的詳細資料,請在 Emulator Suite 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 Authentication

如果您的函式使用 Firebase Admin SDK (9.3.0 以上版本) 寫入 Firebase Authentication,這些寫入作業會傳送至正在執行的 Auth 模擬器。如果這些寫入作業觸發其他函式,系統會在 Cloud Functions 模擬器中執行這些函式。

Firebase 託管

如果您使用 Cloud FunctionsFirebase Hosting 產生動態內容,firebase emulators:start 會將本機 HTTP 函式做為代管的 Proxy。

Firebase 快訊

只要專案包含至少一個支援的 Firebase 快訊觸發條件,模擬器 UI 就會顯示「FireAlerts」分頁。如要模擬觸發快訊:

  1. 開啟「FireAlerts」分頁。這個分頁會顯示下拉式選單,其中列出與觸發條件相關聯的快訊類型 (例如,如果您有 onNewFatalIssuePublished 觸發條件,系統就會顯示 crashlytics.newFatalIssue)。
  2. 選取快訊類型。表單會自動填入預設值,但您可以編輯這些值。您可以編輯事件的欄位 (快訊事件的其他資訊會經過推斷、模擬值或隨機產生)。
  3. 選取「傳送快訊」,將合成快訊傳送至函式模擬器,並在 Firebase 控制台的「快訊」中記錄快訊 (也會記錄在記錄檔中)。

記錄

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

後續步驟

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