【发布时间】:2018-02-14 15:57:11
【问题描述】:
我正在尝试使用 firebase shell 环境在本地对 DialogflowApp 进行单元测试。 (在 cli 中执行 firebase experimental:functions:shell 然后调用我的方法)
我已按照 google https://firebase.google.com/docs/functions/local-emulator 的指南进行操作,但他们不使用 DialogflowApp,其中被调用的函数尝试绑定包含此类意图和参数的请求对象 ->
exports.myFunction = functions.https.onRequest((request, response) => {
const app = new App({ request, response });
function myMethod(app) {
let myArgument = app.getArgument(MY_ARGUMENT);
app.tell('Here we are responding');
}
let actionMap = new Map();
actionMap.set(MYMETHOD_ACTION, myMethod);
app.handleRequest(actionMap);
});
无论我在 CLI 中发送什么请求对象,例如 myFunction(require("../test/testdata.json")),请求主体对象都是空的,例如 body: {},这意味着我无法执行 app.handleRequest() 或 app.getArgument()。我得到的错误信息是
从功能收到的响应:400,操作错误:没有匹配的意图 处理程序:null
我认为如果我使用 Actions on Google -> console.actions.google.com -> Simulator 中显示的 json 请求数据填充 testdata.json,它将是有效数据,但不是。
我的问题是,如何模拟我的请求数据,以便我可以在本地开始单元测试我的 fullfillment 方法?
编辑 1:
firebase > myMethod.post("/").form(require("../test/testdata.json"))
Sent request to function.
firebase > info: User function triggered, starting execution
info: Function crashed
info: TypeError: Cannot destructure property `parameters` of 'undefined' or 'null'.
如果我们查看 dialogflow_app.js,我们可以看到这段用于获取参数值的代码
getArgument (argName) {
debug('getArgument: argName=%s', argName);
if (!argName) {
error('Invalid argument name');
return null;
}
const { parameters } = this.body_.result;
if (parameters && parameters[argName]) {
return parameters[argName];
}
return this.getArgumentCommon(argName);
}
this.body_ 始终只是空的{},无论我在本地运行时如何以及向方法中发送什么。
编辑 3
firebase > myMethod({method: "post",json: true, body: require("../test/testdata.json")})
发送请求以运行。
firebase > info:用户函数触发,开始执行
信息:功能崩溃
信息:TypeError:无法解构“未定义”或“空”的属性parameters。
【问题讨论】:
-
如果变量 MYMETHOD_ACTION 有值,您的代码并不清楚。我从错误消息中可以理解的是,它无法在操作和任何处理请求的方法之间找到匹配项。你能仔细检查一下这个变量是否设置了值吗?
标签: firebase google-cloud-functions chatbot actions-on-google dialogflow-es