用于测试安全规则的Firestore模拟器-运行测试

时间:2020-03-05 07:15:46

标签: google-cloud-firestore firebase-security firebase-cli

我已经按照enter link description here上的说明安装了模拟器,到目前为止,我可以启动它。

在这里和那里选择了一些代码之后,我编写了我的第一个测试,这里是:

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的新手...上面的链接简单地指出

运行一组测试后,您可以访问测试覆盖率报告,该报告显示了如何评估每个安全规则。

所以我想这一定很简单,但是我在任何地方都找不到“运行”命令...

1 个答案:

答案 0 :(得分:1)

如果您有一个nodejs脚本,请使用protocol Optable { func opt() } func onlyCallableByAnOptable<T>( _ value: T) -> T where T: Optable { return value } // Comment the following line to get the errors extension Optional: Optable { func opt() {} } class TestOptable { static func test() { let c = UIColor.blue let s = "hi" let i = Int(1) if let o = onlyCallableByAnOptable(c) { print("color \(o)") } //^ expected ERROR: Argument type 'UIColor' does not conform to expected type 'Optable' if let o = onlyCallableByAnOptable(s) { print("string \(o)") } //^ expected ERROR: Argument type 'String' does not conform to expected type 'Optable' if let o = onlyCallableByAnOptable(i) { print("integer \(o)") } //^ expected ERROR: Argument type 'Int' does not conform to expected type 'Optable' } } 运行它。您必须已安装节点。

如果要与仿真器一起运行脚本,并在脚本完成后关闭仿真器,则链接到的页面将显示:

在许多情况下,您想启动模拟器,运行测试套件并 然后在测试运行后关闭仿真器。你可以这样做 轻松使用emulators:exec命令:

node your-script.js

如果发现文档混乱或不完整,则应使用页面右上方的“发送反馈”按钮。