我正在尝试为我的防火墙云功能设置单元测试。但是得到这个错误
错误:包装函数只适用于
onCall
HTTP函数,而不是onRequest
。
这就是我的app.test.ts的样子
/* 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)看起来像这样
import * as functions from "firebase-functions";
import app from "./app/app";
exports.app = functions.https.onRequest(app);
这里的应用程序是一个简单的快速应用程序。
有人能帮我解决这个问题吗?
发布于 2021-12-22 01:53:04
单元测试包中的wrap()
函数用于重新格式化传递给它的数据,其方式可以被后台事件云函数理解(这些函数在处理时具有一个类似于(data, context) => {}
或(context) => {}
的处理程序签名)。您可以看到该函数正在执行什么这里。
“原始”HTTPS请求函数具有一个类似于(request, response) => {}
的处理程序签名。因为这里没有复杂的处理,所以您可以直接调用函数,直接传入您自己的Request
和Response
对象。文档有关于这方面的更多细节。
// index.js
export myTestFunction = functions.https.onRequest(myExpressApp);
// 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.send
,res.status()
需要返回自己进行链接)。对于这种情况,最好使用快速单元测试库来模拟请求和响应对象,然后将它们传递到要测试的函数中。
https://stackoverflow.com/questions/70442193
复制相似问题