【问题标题】:Unit test Actions on Google Dialogflow locally在本地对 Google Dialogflow 进行单元测试操作
【发布时间】: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


【解决方案1】:

使用 shell 调用 Firebase HTTPS 函数需要 different form。它接受请求模块所做的参数,因此为了模拟 webhook,它将是这样的:

myfunction({
  method: 'POST',
  json: true,
  body: require("../test/testdata.json")
});

这三个参数很重要:

  • 您需要指定这是一个 POST 操作
  • 您需要指明正文将是 JSON。这将发送正确的标头,并且不会尝试将正文作为 x-www-form-urlencoded 发送
  • 您需要包含正文。作为对象是可以的,因为您已将 json 参数设置为 true。

【讨论】:

  • 您好,感谢您的回复。我试图按照指南进行操作,但无论我怎么做,我都会遇到同样的错误。请参阅我使用的 shell 命令的已编辑问题。
  • 您是否使用我建议的确切代码对其进行了测试?
  • 澄清了我为什么使用我所做的参数。
  • 嗨,很抱歉说它不起作用。似乎在我做了 firebase experimental:functions:shell 之后,CLI 并不关心我是否更新了 testdata.json,这让我认为 body 从未设置过。你一直都是对的。谢谢!
  • 这实际上是 node.js 的副作用。一旦你需要()某些东西,结果就会被缓存。很高兴这有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 2017-05-28
  • 2013-04-16
  • 1970-01-01
相关资源
最近更新 更多