【问题标题】:How to write unit tests for MS Bot Builder Node SDK bots?如何为 MS Bot Builder Node SDK 机器人编写单元测试?
【发布时间】:2017-07-20 23:48:36
【问题描述】:

我正在尝试查找 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());

感谢您的意见。

【问题讨论】:

    标签: node.js botframework


    【解决方案1】:

    我认为此时最好的来源是检查 Node.js 中由 Bot Framework 团队在 BotBuilder repo 上完成的单元测试。

    this。他们也在使用Mocha

    【讨论】:

    • 不,我知道。当前版本的文档没有任何关于测试的内容。
    【解决方案2】:

    查看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!');
            })
        ])
    })
    

    有许多额外的功能可用于测试,我建议您参考文档以获得更多信息。

    【讨论】:

      猜你喜欢
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 2018-01-17
      • 2012-01-06
      • 2019-03-17
      相关资源
      最近更新 更多