首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:包装函数只适用于‘onCall’HTTP函数,而不是‘`onRequest`’

错误:包装函数只适用于‘onCall’HTTP函数,而不是‘`onRequest`’
EN

Stack Overflow用户
提问于 2021-12-21 22:21:11
回答 1查看 182关注 0票数 1

我正在尝试为我的防火墙云功能设置单元测试。但是得到这个错误

错误:包装函数只适用于onCall HTTP函数,而不是onRequest

这就是我的app.test.ts的样子

代码语言:javascript
运行
复制
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-unused-vars */
import {stub} from "sinon";
import * as admin from "firebase-admin";
import funcTest from "firebase-functions-test";
admin.initializeApp();

describe("Index Test", function() {
  let adminInitStub:any;
  let appCloudFunction:any;
  const test = funcTest();
  before(async () => {
    adminInitStub = stub(admin, "initializeApp");
    appCloudFunction = await import("../index");
  });

  after(() => {
    adminInitStub.restore();
    test.cleanup();
  });
  it("should always pass", function() {
    const wrapped = test.wrap(appCloudFunction.app);
    wrapped(null);
  });
});

我的云功能(index.ts)看起来像这样

代码语言:javascript
运行
复制
import * as functions from "firebase-functions";
import app from "./app/app";
exports.app = functions.https.onRequest(app);

这里的应用程序是一个简单的快速应用程序。

有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2021-12-22 01:53:04

单元测试包中的wrap()函数用于重新格式化传递给它的数据,其方式可以被后台事件云函数理解(这些函数在处理时具有一个类似于(data, context) => {}(context) => {}的处理程序签名)。您可以看到该函数正在执行什么这里

“原始”HTTPS请求函数具有一个类似于(request, response) => {}的处理程序签名。因为这里没有复杂的处理,所以您可以直接调用函数,直接传入您自己的RequestResponse对象。文档有关于这方面的更多细节。

代码语言:javascript
运行
复制
// index.js
export myTestFunction = functions.https.onRequest(myExpressApp);
代码语言:javascript
运行
复制
// index.test.js
import { myTestFunction } from "./index.js"

const req = {
  url: "https://example.com/path/to/test",
  query: {
    "id": "10"
  }
}

const res = {
  status: () => {} // your test shim
  json: () => {} // your test shim
}

myTestFunction(req, res);

// wait for res to be processed
// do tests

您可能会遇到这样一个简单的响应对象传递问题,因为您在云函数后面使用了Express,并且它执行了许多内部函数调用(例如,res.json将调用res.sendres.status()需要返回自己进行链接)。对于这种情况,最好使用快速单元测试库来模拟请求和响应对象,然后将它们传递到要测试的函数中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70442193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档