Catch up on highlights from Firebase at Google I/O 2023. Learn more

將您的應用程序連接到實時數據庫模擬器

在將您的應用程序連接到實時數據庫模擬器之前,請確保您了解整個 Firebase Local Emulator Suite 工作流程,並安裝和配置Local Emulator Suite 並查看其CLI 命令

選擇一個 Firebase 項目

Firebase Local Emulator Suite 模擬單個 Firebase 項目的產品。

要選擇要使用的項目,請在啟動模擬器之前,在 CLI 中運行firebase use 。或者,您可以將--project標誌傳遞給每個模擬器命令。

Local Emulator Suite 支持模擬真實的Firebase 項目和演示項目。

項目類型特徵與模擬器一起使用
真實的

一個真正的 Firebase 項目是您創建和配置的項目(很可能是通過 Firebase 控制台)。

真實項目具有實時資源,例如數據庫實例、存儲桶、函數或您為該 Firebase 項目設置的任何其他資源。

在處理真實的 Firebase 項目時,您可以為任何或所有受支持的產品運行模擬器。

對於您未模擬的任何產品,您的應用程序和代碼將與實時資源(數據庫實例、存儲桶、函數等)交互。

演示

一個演示 Firebase 項目沒有真正的Firebase 配置,也沒有實時資源。這些項目通常通過代碼實驗室或其他教程訪問。

演示項目的項目 ID 具有demo-前綴。

使用演示 Firebase 項目時,您的應用程序和代碼與模擬器交互。如果您的應用程序嘗試與未運行模擬器的資源進行交互,則該代碼將失敗。

我們建議您盡可能使用演示項目。好處包括:

  • 設置更簡單,因為您無需創建 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);
迅速
    // In almost all cases the ns (namespace) is your project ID.
let db = Database.database(url:"http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE")

Web version 9

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

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

Web version 8

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

無需額外設置即可使用模擬器測試由實時數據庫事件觸發的Cloud Functions。當實時數據庫和 Cloud Functions 模擬器都在運行時,它們會自動協同工作。

管理員 SDK

設置FIREBASE_DATABASE_EMULATOR_HOST環境變量後,Firebase Admin SDK 會自動連接到實時數據庫模擬器:

export FIREBASE_DATABASE_EMULATOR_HOST="localhost:9000"

如果您的代碼在 Cloud Functions 模擬器中運行,您的項目 ID 和其他配置將在調用initalizeApp時自動設置。

如果您希望您的 Admin SDK 代碼連接到在另一個環境中運行的共享模擬器,您將需要指定您使用 Firebase CLI 設置的相同項目 ID 。您可以將項目 ID 直接傳遞給initializeApp或設置GCLOUD_PROJECT環境變量。

Node.js 管理 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);
迅速
// With a DatabaseReference, write nil to clear the database.
    Database.database().reference().setValue(nil);

Web version 9

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 version 8

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

自然地,您的代碼應該使用平台的異步事件處理功能等待刷新完成或失敗的確認。

實施了這樣的步驟後,您可以對測試進行排序並觸發您的功能,並確信舊數據將在運行之間被清除,並且您正在使用新的基線測試配置。

導入導出數據

Firebase 模擬器的數據庫和 Cloud Storage 允許您從正在運行的模擬器實例中導出數據。定義要在單元測試或持續集成工作流程中使用的基線數據集,然後將其導出以在團隊之間共享。

firebase emulators:export ./dir

在測試中,在模擬器啟動時,導入基線數據。

firebase emulators:start --import=./dir

您可以指示模擬器在關閉時導出數據,指定導出路徑或僅使用傳遞給--import標誌的路徑。

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

這些數據導入和導出選項也適用於firebase emulators:exec命令。有關更多信息,請參閱模擬器命令參考

可視化安全規則活動

當您完成原型和測試循環時,您可以使用 Local Emulator Suite 提供的可視化工具和報告。

可視化規則評估

當您將安全規則添加到您的原型時,您可以使用 Local Emulator Suite 工具調試它們。

運行一組測試後,您可以訪問測試覆蓋率報告,其中顯示每個規則的評估方式。要獲取報告,請在模擬器運行時查詢公開的端點。對於瀏覽器友好的版本,請使用以下 URL:

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

這會將您的規則分解為表達式和子表達式,您可以將鼠標懸停在這些表達式和子表達式上以獲取更多信息,包括執行次數和返回值。對於此數據的原始 JSON 版本,請在查詢中包含以下 URL:

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

接下來是什麼?