以下是我们在 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;
}
}
}
生成的报告显示每个规则的使用频率和返回的内容。

如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):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."]]