如何为 MS Bot Builder Node SDK 机器人编写单元测试?
How to write unit tests for MS Bot Builder Node SDK bots?
我正在尝试查找 MS Bot 框架是否提供任何资源/指南来为基于 Node SDK 的机器人编写单元测试(具体来说,我使用的是直线通道)。
如果没有,怎么用Mocha这样的工具来写测试用例来测试各种对话框
我正在使用restify
,如下:
/**-----------------------------------------------------------------
* Setup Chat-Bot
-----------------------------------------------------------------*/
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID || config.appId,
appPassword: process.env.MICROSOFT_APP_PASSWORD || config.appPassword
});
// Initialize bot
var bot = universalBot(connector);
/**-----------------------------------------------------------------
* Setup Server
-----------------------------------------------------------------*/
var server = restify.createServer();
server.listen(process.env.port || 8080, function () {
console.log('%s listening to %s', server.name, server.url);
});
server.pre(restify.pre.sanitizePath());
server.use(restify.queryParser());
/**---------------------------------------------------------------
* Routes
----------------------------------------------------------------*/
server.get('/', function (req, res) {
res.send("Hello from Chatbot API");
});
server.post('/api/messages', connector.listen());
感谢您的输入。
我认为此时最好的来源是检查 Bot Framework 团队在 BotBuilder repo.
上完成的 Node.js
中的单元测试
参见this。他们也在使用 Mocha
。
查看 https://github.com/microsoftly/BotTester。它使使用 Mocha 和 Chai 进行测试比在 botbuilder src 中进行测试要容易得多。例如
it('Can simulate conversation', () => {
bot.dialog('/', [(session) => {
new builder.Prompts.text(session, 'Hi there! Tell me something you like')
}, (session, results) => {
session.send(`${results.response} is pretty cool.`);
new builder.Prompts.text(session, 'Why do you like it?');
}, (session) => session.send('Interesting. Well, that\'s all I have for now')]);
const {
executeDialogTest,
SendMessageToBotDialogStep,
} = testSuiteBuilder(bot);
return executeDialogTest([
new SendMessageToBotDialogStep('Hola!', 'Hi there! Tell me something you like'),
new SendMessageToBotDialogStep('The sky', ['The sky is pretty cool.', 'Why do you like it?']),
new SendMessageToBotDialogStep('It\'s blue', 'Interesting. Well, that\'s all I have for now')
])
})
它还允许在对话中的任何位置检查会话状态。例如
it('Can inspect session state', () => {
bot.dialog('/', [(session) => {
new builder.Prompts.text(session, 'What would you like to set data to?')
}, (session, results) => {
session.userData = { data: results.response };
session.save();
}]);
const {
executeDialogTest,
SendMessageToBotDialogStep,
InspectSessionDialogStep,
} = testSuiteBuilder(bot);
return executeDialogTest([
// having expected responses is not necessary
new SendMessageToBotDialogStep('Start this thing!'),
new SendMessageToBotDialogStep('This is data!'),
new InspectSessionDialogStep((session) => {
expect(session.userData).not.to.be.null;
expect(session.userData.data).to.equal('This is data!');
})
])
})
它还有许多用于测试的附加功能,我建议您参考文档以获得更多见解。
我正在尝试查找 MS Bot 框架是否提供任何资源/指南来为基于 Node SDK 的机器人编写单元测试(具体来说,我使用的是直线通道)。
如果没有,怎么用Mocha这样的工具来写测试用例来测试各种对话框
我正在使用restify
,如下:
/**-----------------------------------------------------------------
* Setup Chat-Bot
-----------------------------------------------------------------*/
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID || config.appId,
appPassword: process.env.MICROSOFT_APP_PASSWORD || config.appPassword
});
// Initialize bot
var bot = universalBot(connector);
/**-----------------------------------------------------------------
* Setup Server
-----------------------------------------------------------------*/
var server = restify.createServer();
server.listen(process.env.port || 8080, function () {
console.log('%s listening to %s', server.name, server.url);
});
server.pre(restify.pre.sanitizePath());
server.use(restify.queryParser());
/**---------------------------------------------------------------
* Routes
----------------------------------------------------------------*/
server.get('/', function (req, res) {
res.send("Hello from Chatbot API");
});
server.post('/api/messages', connector.listen());
感谢您的输入。
我认为此时最好的来源是检查 Bot Framework 团队在 BotBuilder repo.
上完成的Node.js
中的单元测试
参见this。他们也在使用 Mocha
。
查看 https://github.com/microsoftly/BotTester。它使使用 Mocha 和 Chai 进行测试比在 botbuilder src 中进行测试要容易得多。例如
it('Can simulate conversation', () => {
bot.dialog('/', [(session) => {
new builder.Prompts.text(session, 'Hi there! Tell me something you like')
}, (session, results) => {
session.send(`${results.response} is pretty cool.`);
new builder.Prompts.text(session, 'Why do you like it?');
}, (session) => session.send('Interesting. Well, that\'s all I have for now')]);
const {
executeDialogTest,
SendMessageToBotDialogStep,
} = testSuiteBuilder(bot);
return executeDialogTest([
new SendMessageToBotDialogStep('Hola!', 'Hi there! Tell me something you like'),
new SendMessageToBotDialogStep('The sky', ['The sky is pretty cool.', 'Why do you like it?']),
new SendMessageToBotDialogStep('It\'s blue', 'Interesting. Well, that\'s all I have for now')
])
})
它还允许在对话中的任何位置检查会话状态。例如
it('Can inspect session state', () => {
bot.dialog('/', [(session) => {
new builder.Prompts.text(session, 'What would you like to set data to?')
}, (session, results) => {
session.userData = { data: results.response };
session.save();
}]);
const {
executeDialogTest,
SendMessageToBotDialogStep,
InspectSessionDialogStep,
} = testSuiteBuilder(bot);
return executeDialogTest([
// having expected responses is not necessary
new SendMessageToBotDialogStep('Start this thing!'),
new SendMessageToBotDialogStep('This is data!'),
new InspectSessionDialogStep((session) => {
expect(session.userData).not.to.be.null;
expect(session.userData.data).to.equal('This is data!');
})
])
})
它还有许多用于测试的附加功能,我建议您参考文档以获得更多见解。