MS Bot Framework:如何编写按钮测试?

时间:2017-05-18 13:46:03

标签: botframework

我有一个对话框,显示选项列表作为按钮:

  bot.dialog('menu', [
        (session) => {
            builder.Prompts.choice(
                session, 
                'Your choices', 
                ['A', 'B', 'C', 'Exit'], 
                { 
                    listStyle: builder.ListStyle.button 
            });

        },
        (session, results) => {
            let input = _.get(results, 'response.entity', null) || _.get(session, 'message.text', null);
            switch (input) {
                default:
                    session.endDialog("Please chose from available options.");
                    break;
                case 'A':
                    session.send("You chose A");
                    break;
                case 'B':
                    session.beginDialog("You chose B");
                    break;
                case 'C':
                    session.beginDialog("You chose C");
                    break;
                case 'Exit':
                    session.endDialog("Thanks for using menu!");
                    break;
        }
    }]
    ).triggerAction({
        matches: /^(menu)$/i
    })
}

当我按下按钮或通过文本输入输入选项时,这是有效的。

但是,当我通过自动测试测试对话框时,它对文本输入不满意。

以下是测试:

describe('menuDialog', function (done) {
    this.timeout(50000);

    // Test case for menu
    it('should present a list of options', function (done) {
        var step = 0;
        var connector = new builder.ConsoleConnector();
        var bot = universalBot(connector);

        // Register a dialog to test
        menuDialog(bot, builder);


        bot.on('send', function (message) {
            switch (++step) {
                case 1:
                    console.log(`Step : ${step}`);
                    assert(message.text == 'Your Choices');
                    connector.processMessage('A');
                    done()
                    break;
                case 2:
                    console.log("STEP : ", step)
                    assert(message.text == 'You Chose A');
                    done()
                    break;
           }
       });
        connector.processMessage('menu');
    });

});

测试失败,步骤2中的message.text为:

Oops something went wrong and we need to start over.

如果不通过connector.processMessage()

,如何测试按钮?

由于

1 个答案:

答案 0 :(得分:0)

您需要将机器人的对话框代码放在测试块中,以便测试可以访问它。

以下是测试意图的工作版本:

// BEGIN buttons.test.js

var builder = require('botbuilder');
var assert = require('assert');

describe('menuDialog', function() {
    this.timeout(5000);
    // Test case for menu
    it('should present a list of options', function (done) {
        var step = 0;
        var connector = new builder.ConsoleConnector();
        //var bot = universalBot(connector);
        var bot = new builder.UniversalBot(connector);

        bot.dialog('/', [
            (session) => {
                builder.Prompts.choice(session, 'Your choices', ['A', 'B', 'C', 'Exit'], {listStyle: builder.ListStyle.button});

            },
            (session, results) => {
                console.log('selected option: ' + results.response.entity);

                switch (results.response.entity) {
                    case 'A':
                        session.send("You picked A");
                        break;
                    case 'B':
                        session.beginDialog("You picked B");
                        break;
                    case 'C':
                        session.beginDialog("You picked C");
                        break;
                    case 'Exit':
                        session.endDialog("You picked Exit");
                        break;
                    default:
                        session.endDialog("Please chose from available options.");
                        break;
                }
            }
        ]);

        bot.on('send', function (message) {
            switch (++step) {
                case 1:
                    console.log(`Step: ${step}`);
                    assert(message.text == 'Your choices');
                    connector.processMessage('A');
                    done();
                    break;
                case 2:
                    console.log(`Step: ${step}`);
                    assert(message.text == 'You picked A');
                    done();
                    break;
            }
        });
        connector.processMessage('start');
    });   
});

// END buttons.test.js
// to run: mocha buttons.test.js