[英]How can I call Discord.js functions from a lib.js file into index.js?
[英]Unit testing functions from index.js
我在Dialogflow的实现部分添加了一个add函数,该函数在调用时工作得很好。
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
// intentMap.set('your intent name here', yourFunctionHandler);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});
function myadd(a,b){
return a+b
}
我想用Mocha对该功能进行单元测试。 为此,我必须导出测试文件的功能,因此我尝试通过以下方式进行操作:
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
// intentMap.set('your intent name here', yourFunctionHandler);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});
exports.dialogflowFirebaseFulfillment = {
myaddfunction: (a, b) => {
return a + b
}
}
此更改将导致错误消息: 函数在加载用户代码时失败。 错误消息:从文件index.js导出为dialogflowFirebaseFulfillment的函数必须是函数类型。 得到了:object s
如何才能从index.js调用我的函数并同时导出以进行单元测试? 我想我在这里缺少一个概念。
我尝试了以下建议:
import isEmpty from './works';
在同一文件夹中的works.js文件中,我具有以下代码:
function isEmpty(obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key))
return false;
}
return true;
}
export {
isEmpty
};
但是然后我得到了错误:
从'./works'导入isEmpty; ^^^^^^^^
语法错误:意外的标识符
您可以通过多种方式编写此代码。 但是,这可能会有所帮助。
function myadd(a,b){
return a+b;
}
export {
myadd
};
现在,在其他文件上,您可以:
import myadd from "path to your module goes here";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.