在onUpdate Trigger Typescript中未定义使用Jest和context.params.userId测试我的cloudfunction

问题描述

我正在为Firebase cloudfunction触发器编写测试。并且我遵循了这种方式tutorial,第一个我写得很好,在用户创建新的帖子时调用触发器,它在用户文档中创建了一个新文档,并且可以正常工作。

现在,如果用户更新存储在users/GC7QEZGBfcYw1tYXyoQfbsRw8ll1中的个人信息,我将在userUpdate上触发其第二次。它做很多事情,例如获取所有朋友并在所有朋友中更新该用户的基本信息并在帖子中更新,但是我收到错误消息,即我未定义我在路径中提供的userId

我不知道我在想什么。

我的第二个问题:是否可以在Firebase模拟器而不是在线Firebase中更新数据?

这是我所做的

initializeApp

import * as admin from "firebase-admin";
admin.initializeApp({
  credential: admin.credential.cert({
    projectId: "firebase project ID",privateKey:
      "-----BEGIN PRIVATE KEY-----\$Key=\n-----END PRIVATE KEY-----\n",clientEmail:
      "clientEmail from firebase",}),databaseURL: "firebase database url",});
import functions from "firebase-functions-test";
const testEnv = functions({
  databaseURL: "firebase database url",projectId: "firebase project id",});

测试代码

import "jest";
describe("On Update User",() => {
  test("It update user info in all friends & Events",async () => {
    const wrapped = testEnv.wrap(onUpdateUser);
    const path = "users/GC7QEZGBfcYw1tYXyoQfbsRw8ll1";
    const data1 = {
      avatarUrl: "avatarUrl",bio: "bio",last_name_lower: "ben",name: "Ben",name_lower: "ben",username: "ben",};
    const data2 = {
      avatarUrl: "avatarUrl",last_name_lower: "khan",name: "Khan",name_lower: "khan",username: "khan007",};
    const snap1 = testEnv.firestore.makeDocumentSnapshot(data1,path);
    const snap2 = testEnv.firestore.makeDocumentSnapshot(data2,path);
    const change = testEnv.makeChange(snap1,snap2);

    // Execute it
    await wrapped(change);

  });
});

我使用Firebase模拟器测试所有触发器,并且一切正常,但使用玩笑我正面临此问题

我得到的第一个错误

参数“ documentPath”的值不是有效的资源路径。路径必须是非空字符串。

firestore.collection("friends").doc(userId)

at Object.validateResourcePath (../node_modules/@google-cloud/firestore/build/src/path.js:406:15)
      at CollectionReference.doc (../node_modules/@google-cloud/firestore/build/src/reference.js:1982:20)
      at Function.ref (models/friends.ts:44:44)
      at Function.descriptor.value (decorators/log.ts:18:31)
      at Function.fromId (models/friends.ts:33:29)
      at Function.descriptor.value (decorators/log.ts:18:31)
      at Function.exports.onUpdateUser.functions.firestore.document.onUpdate [as run] (users/onUpdateUser.ts:23:37)
      at wrapped (../node_modules/firebase-functions-test/lib/main.js:71:30)
      at Object.test (test/userUpdate.test.ts:35:11)

我也使用装饰器记录参数

并记录

arg 1未定义

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)