本地对Google Dialogflow进行单元测试操作 [英] Unit test Actions on Google Dialogflow locally
问题描述
我正在尝试使用Firebase Shell环境在本地对 DialogflowApp
进行单元测试。 (在cli做firebase实验性:functions:shell,然后调用我的方法)
I'm trying to unit test a DialogflowApp
locally by using the firebase shell environment. (in a cli do firebase experimental:functions:shell and then call my methods)
我遵循了Google https://firebase.google.com/docs/functions/local-emulator ,但是他们不使用DialogflowApp被调用的函数尝试绑定包含如下意图和参数的请求对象->
I have followed this guide by google https://firebase.google.com/docs/functions/local-emulator but they don't use the DialogflowApp where the invoked function tries to bind a request object containing intents and parameters like this ->
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()
。我收到的错误消息是
Regardless of what request object I send in the CLI, like this myFunction(require("../test/testdata.json"))
, the request body object is empty, like this body: {}
which means I can't do app.handleRequest()
or app.getArgument()
. The error message I get is
从功能接收到响应:400,操作错误:没有匹配的意图
处理程序,用于:null
RESPONSE RECEIVED FROM FUNCTION: 400, Action Error: no matching intent handler for: null
我以为,如果我用Google Actions-> console.actions.google.com中显示的json请求数据填充testdata.json, ->模拟器,它将是有效数据,但没有。
I thought that if I populated testdata.json with the json request data shown in Actions on Google -> console.actions.google.com -> Simulator it would be valid data but no.
我的问题是,我该如何模拟我的请求数据,以便可以在本地开始对填充方法进行单元测试?
My question is, how can i mock my request data so that I can start unit testing my fullfillment methods locally?
编辑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,我们可以看到这段代码用于获取参数值
if we look in dialogflow_app.js we can see this code for fetching an argument value
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_始终为空 {}
,而不管我在本地运行时如何发送给该方法。
this.body_ is always just empty {}
, regardless of how and what I send into the method when running locally.
编辑3
firebase > myMethod({method: "post",json: true, body: require("../test/testdata.json")})
已发送功能请求。
firebase>信息:触发用户功能,开始执行
信息:函数崩溃
信息:TypeError:无法解构'undefined'的属性 parameters
'或'null'。
Sent request to function.
firebase > info: User function triggered, starting execution
info: Function crashed
info: TypeError: Cannot destructure property parameters
of 'undefined' or 'null'.
推荐答案
使用shell调用Firebase HTTPS函数需要其他格式。它需要请求模块执行的参数,因此,为了模拟Webhook,将类似于以下内容:
Invoking a Firebase HTTPS function using the shell requires a different form. It takes the parameters that the request module does, so in order to emulate a webhook, it will be something like this:
myfunction({
method: 'POST',
json: true,
body: require("../test/testdata.json")
});
这三个参数很重要:
- 您需要指定这是一个POST操作
- 您需要指明主体为JSON。这样会发送正确的标头,并且不会尝试将正文作为x-www-form-urlencoded
- 发送。您需要包括正文。作为对象就可以了,因为您已将json参数设置为true。
这篇关于本地对Google Dialogflow进行单元测试操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!