將應用程式連線至即時資料庫模擬器

將應用程式連線至即時資料庫模擬器之前,請務必瞭解整體 Firebase 本機模擬器套件的工作流程,並安裝及設定本機模擬器套件並查看其 CLI 指令

選擇 Firebase 專案

Firebase 本機模擬器套件可模擬單一 Firebase 專案中的產品。

如要選取要使用的專案,請先透過 CLI 在工作目錄中執行 firebase use,再啟動模擬器。或者,您也可以將 --project 標記傳遞至每個模擬器指令。

本機模擬器套件支援模擬「實際」的 Firebase 專案和示範專案。

專案類型 功能與特色 與模擬器搭配使用
真實

真正的 Firebase 專案是您建立及設定的專案 (很有可能透過 Firebase 控制台進行)。

真實專案包含使用中的資源,例如資料庫執行個體、儲存空間值區、函式,或是您為該 Firebase 專案設定的任何其他資源。

使用實際的 Firebase 專案時,您可以針對任何或所有支援的產品執行模擬器。

針對並非您要模擬的任何產品,應用程式和程式碼會與即時資源 (資料庫執行個體、儲存空間值區、函式等) 互動。

示範

示範 Firebase 專案沒有實際的 Firebase 設定,也沒有即時資源。一般來說,您可以透過程式碼研究室或其他教學課程存取這些專案。

示範專案的專案 ID 含有 demo- 前置字串。

使用示範 Firebase 專案時,您的應用程式和程式碼「只會」與模擬器互動。如果應用程式嘗試與未執行模擬器的資源互動,該程式碼將會失敗。

建議您盡可能使用示範專案。Meet 具備以下優點:

  • 設定更簡單,因為您無須建立 Firebase 專案即可執行模擬器
  • 安全性較高,因為如果程式碼意外叫用非模擬的 (正式環境) 資源,就無法變更資料、使用及計費
  • 改善離線支援功能,因為不需透過網際網路即可下載 SDK 設定。

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

Android、Apple 平台和 Web SDK

設定應用程式內設定或測試類別,與即時資料庫互動,方法如下。

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 database = Firebase.database
database.useEmulator("10.0.2.2", 9000)
Java
// 10.0.2.2 is the special IP address to connect to the 'localhost' of
// the host computer from an Android emulator.
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.useEmulator("10.0.2.2", 9000);
Swift
    // In almost all cases the ns (namespace) is your project ID.
let db = Database.database(url:"http://127.0.0.1:9000?ns=YOUR_DATABASE_NAMESPACE")

Web

import { getDatabase, connectDatabaseEmulator } from "firebase/database";

const db = getDatabase();
if (location.hostname === "localhost") {
  // Point to the RTDB emulator running on localhost.
  connectDatabaseEmulator(db, "127.0.0.1", 9000);
} 

Web

var db = firebase.database();
if (location.hostname === "localhost") {
  // Point to the RTDB emulator running on localhost.
  db.useEmulator("127.0.0.1", 9000);
} 

如要測試由即時資料庫事件觸發的 Cloud Functions,不需要進行額外設定。當即時資料庫和 Cloud Functions 模擬器同時執行時,它們會自動搭配運作。

管理員 SDK

如果設定了 FIREBASE_DATABASE_EMULATOR_HOST 環境變數,Firebase Admin SDK 會自動連線至即時資料庫模擬器:

export FIREBASE_DATABASE_EMULATOR_HOST="127.0.0.1:9000"

如果程式碼是在 Cloud Functions 模擬器中執行,系統將在呼叫 initializeApp 時自動設定專案 ID 和其他設定。

如要讓 Admin SDK 程式碼連線至在其他環境中執行的共用模擬器,您必須指定使用 Firebase CLI 設定的相同專案 ID。您可以將專案 ID 直接傳遞至 initializeApp,或是設定 GCLOUD_PROJECT 環境變數。

Node.js Admin SDK
admin.initializeApp({ projectId: "your-project-id" });
環境變數
export GCLOUD_PROJECT="your-project-id"

清除測試之間的資料庫

如要清除活動的即時資料庫,您可以清除資料庫參考資料。您可以將這個方法做為直接關閉模擬器程序的替代方法。

Kotlin+KTX
// With a DatabaseReference, write null to clear the database.
database.reference.setValue(null)
Java
// With a DatabaseReference, write null to clear the database.
database.getReference().setValue(null);
Swift
// With a DatabaseReference, write nil to clear the database.
    Database.database().reference().setValue(nil);

Web

import { getDatabase, ref, set } from "firebase/database";

// With a database Reference, write null to clear the database.
const db = getDatabase();
set(ref(db), null);

Web

// With a database Reference, write null to clear the database.
firebase.database().ref().set(null);

一般來說,程式碼應利用平台的非同步事件處理功能,等待確認清除完畢或失敗。

完成上述步驟後,您就可以將測試排序並觸發函式,不必擔心舊資料會在執行期間清除,且使用新的基準測試設定。

匯入及匯出資料

資料庫和 Cloud Storage for Firebase 模擬器可讓您從執行中的模擬器執行個體匯出資料。定義要在單元測試或持續整合工作流程中使用的基準資料集,然後匯出供團隊成員共用。

firebase emulators:export ./dir

在測試中,在模擬器啟動時匯入基準資料。

firebase emulators:start --import=./dir

您可以指示模擬器在關機時匯出資料,方法是指定匯出路徑,或者直接使用傳遞至 --import 標記的路徑。

firebase emulators:start --import=./dir --export-on-exit

這些資料匯入和匯出選項也適用於 firebase emulators:exec 指令。詳情請參閱模擬器指令參考資料

以圖表呈現安全性規則活動

處理原型和測試迴圈時,您可以使用本機模擬器套件提供的視覺化工具和報表。

視覺化呈現規則評估

將安全性規則新增至原型後,即可使用本機模擬器套件工具進行偵錯。

執行一系列的測試後,您可以存取測試涵蓋範圍報表,其中會顯示各規則的評估方式。如要取得報告,請在模擬器執行期間查詢已公開的端點。如需適用於瀏覽器的版本,請使用下列網址:

http://localhost:9000/.inspect/coverage?ns=<database_name>

這會將規則分為不同的運算式和子運算式,您可以將滑鼠遊標移到運算式和子運算式上,取得更多資訊,包括執行作業數和傳回的值。針對這項資料的原始 JSON 版本,請在查詢中加入下列網址:

http://localhost:9000/.inspect/coverage.json?ns=<database_name>

接下來呢?