vivekanon
vivekanon

Reputation: 1823

How to write unit tests for MS Bot Builder Node SDK bots?

I'm trying to find if the MS Bot framework provides any resource / guidelines for writing unit tests for bots based on Node SDK (Specifically, I using the direct line channel).

If not, how can tools like Mocha be used to write test cases to test various dialogs.

I'm using restify, as below:

/**-----------------------------------------------------------------
 * 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());

Thanks for you input.

Upvotes: 1

Views: 1033

Answers (2)

Matt
Matt

Reputation: 21

Check out https://github.com/microsoftly/BotTester. It makes testing with Mocha and Chai much easier than the tests in the botbuilder src. E.g.

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 also allows for inspection of session state at any point within the dialog. E.g

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

Theres a bunch of additional functionality for testing that it works with, I'd suggest referring to the docs for a little more insight.

Upvotes: 2

Ezequiel Jadib
Ezequiel Jadib

Reputation: 14787

I think the best source at this point would be to check the unit tests in Node.js done by the Bot Framework team on the BotBuilder repo.

See this. They are using Mocha too.

enter image description here

Upvotes: 5

Related Questions