且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在没有对话框的情况下对我的机器人进行单元测试?(C#)

更新时间:2023-09-10 09:04:52

我无法帮助您使用C#的语法,但是我的nodejs bot中有一些不是对话框的测试,这可能会对您有所帮助.本质上,您只需创建一个TestAdapter并将其与活动一起传递给您的机器人.例如,这是我的dispatchBot.test.js文件的一部分:

I can't help you with the syntax for C#, but I have some tests in my nodejs bot that are not dialogs and this may help you. Essentially, you just create a TestAdapter and pass that with an activity to your bot. For example, here is part of my dispatchBot.test.js file:

const { TestAdapter, ActivityTypes, TurnContext, ConversationState, MemoryStorage, UserState } = require('botbuilder');
const { DispatchBot } = require('../../bots/dispatchBot');
const assert = require('assert');
const nock = require('nock');
require('dotenv').config({ path: './.env' });

// Tests using mocha framework
describe('Dispatch Bot Tests', () => {

    const testAdapter = new TestAdapter();
    async function processActivity(activity, bot) {
        const context = new TurnContext(testAdapter, activity);
        await bot.run(context);
    }

    it('QnA Generic Response'), async () => {
        const memoryStorage = new MemoryStorage();
        let bot = new DispatchBot(new ConversationState(memoryStorage), new UserState(memoryStorage), appInsightsClient);

        // Create message activity
        const messageActivity = {
            type: ActivityTypes.Message,
            channelId: 'test',
            conversation: {
                id: 'someId'
            },
            from: { id: 'theUser' },
            recipient: { id: 'theBot' },
            text: `This is an unrecognized QnA utterance for testing`
        };

        // Send the conversation update activity to the bot.
        await processActivity(messageActivity, bot);

        // Assert we got the right response
        let reply = testAdapter.activityBuffer.shift();
        assert.equal(reply.text,defaultQnaResponse);
    });
});

如果您的漫游器以多种活动响应,则即使只是清除缓冲区,您也只需继续使用 testAdapter.activityBuffer.shift().否则,它将那些消息保留在缓冲区中以供后续测试.希望这会有所帮助!

If your bot responds with multiple activities, you just need to continue to use testAdapter.activityBuffer.shift(), even if just to clear the buffer. Otherwise it would keep those messages in the buffer for subsequent tests. Hopefully this will help!