javascript Firestore单元测试问题:应为类型“DocumentReference”,但它是:自定义Firestore对象

qcuzuvrc  于 2023-03-21  发布在  Java
关注(0)|答案(1)|浏览(108)

我想运行firestore的单元测试。我有以下代码:

import { getDoc, setDoc } from "@firebase/firestore";
import {
  assertFails,
  assertSucceeds,
  initializeTestEnvironment,
  RulesTestEnvironment,
} from "@firebase/rules-unit-testing"

describe('Creating a user', () => {
  let alice;
  let anon;
  let testEnv;

  beforeAll(async () => {
    testEnv = await initializeTestEnvironment({
      projectId: "PROJECT_ID"
    })
  })

  beforeEach(() => {
    alice = testEnv.authenticatedContext("alice", { user_id: "1" });
    anon = testEnv.unauthenticatedContext();
  });

  test('alice can create a user', async () => {
    await assertSucceeds(setDoc(alice.firestore(), '/users/alice'), { "name": "alice" });
  })
});

这是根据official documentation,他们建议这个示例代码:

// Assuming a Firestore app and the Firestore emulator for this example
import { setDoc } from "firebase/firestore";

const alice = testEnv.authenticatedContext("alice", { … });
// Use the Firestore instance associated with this context
await assertSucceeds(setDoc(alice.firestore(), '/users/alice'), { ... });

但是,当我运行this时,我得到了以下错误:
Firebase错误:应为类型“DocumentReference”,但它是:自定义Firestore对象
在此行:await assertSucceeds(setDoc(alice.firestore(), '/users/alice'), { "name": "alice" });
有人知道我做错了什么吗?我已经看过官方文档,但我不清楚为什么这不起作用。
npm依赖项:

├── @babel/cli@7.21.0
├── @babel/core@7.21.3
├── @babel/plugin-transform-modules-commonjs@7.21.2
├── @babel/preset-env@7.20.2
├── @firebase/rules-unit-testing@2.0.7
├── firebase@9.18.0
└── jest@29.5.0
6rqinv9w

6rqinv9w1#

我找不到您在提供的link中提到的示例。
这是根据官方文档,他们建议这个示例代码:
setDoc接受一个DocumentReference作为第一个参数,这就是你得到的异常的意思。

相关问题