在本機執行函式

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

  • HTTPS 函式
  • 可呼叫的函式
  • 工作佇列函式
  • Firebase AuthenticationRealtime Database 觸發的背景函式。 Cloud 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 以上版本。

設定管理員憑證 (選用)

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

  • Cloud FirestoreRealtime Database 觸發條件已有足夠的憑證,且「不需要」進行額外設定。
  • 至於其他所有 API (包括 AuthenticationFCM 等 Firebase API),或 Cloud Translation 或 Cloud Speech 等 Google API,都必須完成本節所述的設定步驟。無論使用 Cloud Functions 殼層還是 firebase emulators:start,都適用。

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

  1. 開啟 服務帳戶窗格 Google Cloud控制台中就能保留這項資訊
  2. 確認已選取 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。舉例來說 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 指令會啟動 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

檢測應用程式以執行工作佇列函式模擬

模擬器會根據觸發條件自動設定模擬的工作佇列 定義,而 Admin SDK 會將已加入佇列的要求重新轉送至模擬器, 它會透過 CLOUD_TASKS_EMULATOR_HOST 環境偵測是否正在運作 變數。

請注意,在正式環境中使用的調度系統比 因此,請不要預期 才能精確模擬生產環境當中的參數 模擬器提供調度工作的頻率上限 然後重試。

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

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

  • Realtime Database 模擬器
  • Cloud Firestore 模擬器
  • Authentication 模擬器
  • Pub/Sub 模擬器
  • Firebase 快訊模擬器

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

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

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

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

Cloud Functions 執行階段會設定 EVENTARC_EMULATOR 環境 將 Eventarc 模擬器設為 localhost:9299 正在運作。Firebase Admin SDK 會自動連線至 Eventarc 在已設定 EVENTARC_EMULATOR 環境變數的情況下模擬器。你可以 修改預設通訊埠,如設定 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,系統會將這些寫入內容傳送至驗證模擬器 以及是否正在運作如果這些寫入動作觸發了後續函式 它們會在 Cloud Functions 模擬器中執行。

Firebase 託管

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

Firebase 快訊

在含有至少一項支援 Firebase 快訊觸發條件的任何專案中, 模擬器 UI 包含「FireAlerts」分頁。如要模擬快訊觸發條件,請按照下列步驟操作:

  1. 開啟「FireAlerts」分頁。這個分頁會顯示已填入 包含相關觸發條件的快訊類型 (例如 onNewFatalIssuePublished 觸發條件,則當機 lytics.newFatalIssue 顯示)。
  2. 選取快訊類型。系統會在表單中自動填入預設值, 則可以編輯您可以編輯事件的欄位 (其他 來自於警示事件的資訊是推論、模擬值或 隨機產生)。
  3. 選取「Send Alert」,將綜合快訊傳送至函式模擬器。 Firebase 控制台的快訊提供記錄功能 以及記錄中。

記錄

模擬器會將函式記錄串流至終端機視窗 此程序的第一步 是將程式碼簽入執行所有單元測試的存放區中並顯示 console.log()console.info()、 函式中的 console.error()console.warn() 陳述式。

後續步驟

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