以下是我们在 I/O 大会上宣布的所有内容,从新的 Firebase Studio 功能到集成 AI 的更多方式,内容非常丰富。
阅读博客。
產生測試報告
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Cloud Firestore 和 Realtime Database 都採用功能強大且簡潔的規則語言,專門用於管理資訊安全和存取控制。不過,隨著規則越來越長且複雜,您可能需要協助偵錯規則行為中的錯誤。
Firebase 模擬器可產生規則涵蓋範圍報表,因此您重現錯誤時,可以準確瞭解每個子運算式評估的結果。報表也會提供資訊,說明每個測試案例使用規則的頻率,例如傳統的「行涵蓋範圍」技術。
產生報表
執行一系列測試後,您就能存取測試涵蓋範圍報表,瞭解每項安全性規則的評估方式。
如要取得報表,請在模擬器執行時查詢公開端點。如要使用瀏覽器友善版本,請前往下列網址:
Cloud Firestore
http://localhost:8080/emulator/v1/projects/<database_name>:ruleCoverage.html
Realtime Database
http://localhost:9000/.inspect/coverage?ns=<database_name>
這會將規則拆解為運算式和子運算式,您可以將滑鼠游標懸停在這些項目上,查看更多資訊,包括評估次數和傳回的值。如要取得這項資料的原始 JSON 版本,請在查詢中加入下列網址:
Cloud Firestore
http://localhost:8080/emulator/v1/projects/<database_name>:ruleCoverage
Realtime Database
http://localhost:9000/.inspect/coverage.json?ns=<database_name>
偵錯規則範例
如要輕鬆產生測試報告,請使用 GitHub 上的模擬器快速入門指南:Cloud Firestore 和 Realtime Database。這些快速入門導覽課程會逐步說明如何正確安裝及初始化模擬器,然後從範例規則集產生範例測試。
以使用 Cloud Firestore 的範例應用程式為例,這個應用程式會計算使用者點選按鈕的次數。應用程式採用下列規則:
Cloud Firestore
service cloud.firestore {
match /databases/{database}/documents {
match /counters/{counter} {
allow read;
allow write: if request.resource.data.value == resource.data.value +1;
}
}
}
如要偵錯上述規則中的錯誤,請使用下列 JavaScript 測試範例:
const counter0 = db.collection("counters").doc("0");
await firebase.assertSucceeds(counter0.set({value: 0}));
模擬器會產生報表,網址如上所示:
http://localhost:8080/emulator/v1/projects/<database_name>:ruleCoverage.html
報表會顯示下列未定義和空值錯誤:

這個特定範例的問題在於,規則無法區分建立文件和更新文件。因此,如果文件不存在,系統就不允許寫入,且由於文件不存在,因此無法建立。將「寫入」區分為兩個更具體的作業:「建立」和「更新」,即可解決問題。
Cloud Firestore
service cloud.firestore {
match /databases/{database}/documents {
match /counters/{counter} {
allow read;
allow create: if request.resource.data.value == 0;
allow update: if request.resource.data.value == resource.data.value +1;
}
}
}
產生的報表會顯示每項規則的使用頻率和傳回的內容。

除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nCloud Firestore and Realtime Database both rely on powerful, concise rules languages\nspecifically created to govern information security and access control. However,\nas rules get longer and more complex, you might need some help debugging errors\nin their behavior.\n\nThe Firebase Emulators include the ability to generate rule coverage reports, so you\ncan see see exactly what each subexpression evaluated to when you reproduce\nan error. The reports also provide information about how frequently each test\ncase used a rule, like traditional \"line coverage\" techniques.\n\nGenerate a report\n\nAfter running a suite of tests, you can access test\ncoverage reports that show how each of your security rules was evaluated.\n\nTo get the reports, query an exposed endpoint on the emulator while\nit's running. For a browser-friendly version, use the following URL: \n\nCloud Firestore \n\n```scdoc\nhttp://localhost:8080/emulator/v1/projects/\u003cdatabase_name\u003e:ruleCoverage.html\n \n```\n\nRealtime Database \n\n```scdoc\nhttp://localhost:9000/.inspect/coverage?ns=\u003cdatabase_name\u003e\n \n```\n\nThis breaks your rules into expressions and subexpressions that you can\nmouseover for more information, including number of evaluations and values\nreturned. For the raw JSON version of this data, include the following URL\nin your query: \n\nCloud Firestore \n\n```scdoc\nhttp://localhost:8080/emulator/v1/projects/\u003cdatabase_name\u003e:ruleCoverage\n \n```\n\nRealtime Database \n\n```scdoc\nhttp://localhost:9000/.inspect/coverage.json?ns=\u003cdatabase_name\u003e\n \n```\n\nDebugging example rules\n\nTo easily generate a test report, use the emulator quickstarts available on\nGitHub for [Cloud Firestore](https://github.com/firebase/quickstart-testing/) and [Realtime Database](https://github.com/firebase/quickstart-testing/).\nThese quickstarts guide you through properly installing\nand initializing the emulators, then generating sample tests from an example\nset of rules.\n\nConsider an example app using Cloud Firestore that counts how many times users\nclick a button. The app employs the following rules: \n\nCloud Firestore \n\n```css+lasso\n service cloud.firestore {\n match /databases/{database}/documents {\n match /counters/{counter} {\n allow read;\n allow write: if request.resource.data.value == resource.data.value +1;\n }\n }\n }\n \n```\n\nTo debug the errors in the rules shown above, use the following sample\nJavaScript test: \n\n const counter0 = db.collection(\"counters\").doc(\"0\");\n await firebase.assertSucceeds(counter0.set({value: 0}));\n\nThe emulator generates a report available at the URL noted above: \n\n```scdoc\nhttp://localhost:8080/emulator/v1/projects/\u003cdatabase_name\u003e:ruleCoverage.html\n```\n\nThe report shows the following undefined and null-value errors:\n\nThe problem with this specific example is that the rules don't differentiate\nbetween creating the document and updating the document. Consequently, the\nwrite isn't allowed if the document doesn't exist, and the document can't be\ncreated because it doesn't exist. Differentiating the \"write\" into two\nmore specific operations --- \"create\" and \"update\" --- solves the problem. \n\nCloud Firestore \n\n```css+lasso\n service cloud.firestore {\n match /databases/{database}/documents {\n match /counters/{counter} {\n allow read;\n allow create: if request.resource.data.value == 0;\n allow update: if request.resource.data.value == resource.data.value +1;\n }\n }\n }\n \n```\n\nThe generated report shows how frequently each rule was used and what was\nreturned."]]