Google cloud firestore Firestore emulator用于测试安全规则-运行测试
我已经按照上的说明安装了仿真器,我可以启动它,到目前为止还不错 在这里和那里挑选了一些代码之后,我编写了我的第一个测试,如下所示:Google cloud firestore Firestore emulator用于测试安全规则-运行测试,google-cloud-firestore,firebase-security,firebase-cli,Google Cloud Firestore,Firebase Security,Firebase Cli,我已经按照上的说明安装了仿真器,我可以启动它,到目前为止还不错 在这里和那里挑选了一些代码之后,我编写了我的第一个测试,如下所示: import * as firebasetesting from '@firebase/testing'; import * as firebase from 'firebase'; import * as fs from 'fs'; const projectId = 'my-firebase-project'; const rules = fs.readFil
import * as firebasetesting from '@firebase/testing';
import * as firebase from 'firebase';
import * as fs from 'fs';
const projectId = 'my-firebase-project';
const rules = fs.readFileSync('firestore.rules', 'utf8');
beforeAll(async () => {
// Make your test app load your firestore rules
await firebasetesting.loadFirestoreRules({ projectId, rules });
});
beforeEach(async () => {
// Reset our data from our test database
await firebasetesting.clearFirestoreData({ projectId });
});
after(async () => {
// Shut down all testing Firestore applications after testing is done.
await Promise.all(firebasetesting.apps().map(app => app.delete()));
});
describe("TRACKERS AND ALLIES", () => {
it('TRACKER UP', async () => {
let user = {username: "Bob", uid: 'bobuid'}
let target = { username: "Alice", uid: 'aliceuid'}
const auth = { uid: bob.uid, token: {isadmin: false} };
const app = firebasetesting.initializeTestApp({ projectId, auth }).firestore();
const ref = app.doc('users/'+ user.uid + '/contact/' + target.uid);
await firebasetesting.assertSucceeds(ref.update({ up: true, username: target.uid, timestamp: firebase.firestore.FieldValue.serverTimestamp() }));
});
})
我的问题很简单:我如何运行它
编辑:我可能只是补充说我是Firestore和Javascript的新手。。。上面的链接只是说明
在运行一组测试之后,您可以访问测试覆盖率报告,该报告显示如何评估每个安全规则
所以我想它一定很简单,但我在任何地方都找不到run命令…如果您有nodejs脚本,请使用node your-script.js运行它。必须已安装节点 如果要随emulator一起运行脚本,并在脚本完成后关闭emulator,则链接到的页面会显示: 在许多情况下,您需要启动仿真器,运行测试套件,然后 然后在测试运行后关闭模拟器。你可以这样做 轻松使用模拟器:exec命令:
firebase emulators:exec --only firestore "./my-test-script.sh"
如果您发现文档混乱或不完整,您应该使用页面右上角的“发送反馈”按钮。这让我开始了,我现在遇到了一些错误,如果我没有解决这些问题,我将发布一个新问题因此。。。缺少了很多东西,首先上面的代码使用了一个测试框架——我一开始并不理解。我已经安装了mocha并取得了进展,但是emulator无法读取我在auth变量中定义的自定义声明,我得到一个错误属性isadmin在对象上未定义