如何对单个会话语句进行单元测试

耳环

我正在使用机器人和 Microsoft Bot Framework。我使用 DispatchBot 模板来生成我的机器人。https://docs.microsoft.com/de-de/azure/bot-service/bot-builder-tutorial-dispatch?view=azure-bot-service-4.0&tabs=cs

对于会话测试,我想创建单元测试。因此,我使用此文档来收集一些信息(https://docs.microsoft.com/de-de/azure/bot-service/unit-test-bots?view=azure-bot-service-4.0&tabs=csharp

问题是我不想测试对话框,而是一个单一的陈述(一个问题和正确的答案)我该如何实现?

Here you can see the Start of my Dispatchbot.cs file where the magic happens (search of the correct Knowledge Base etc.)

mdrichardson

Here's a link to how we create tests for CoreBot. The part you're most likely interested in is testing things under the /Bots directory. Based off of the test code you can find there, you likely want something like:

using System;
using System.Threading;
using System.Threading.Tasks;
using CoreBot.Tests.Common;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.BotBuilderSamples.Bots;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;

namespace KJCBOT_Tests
{
    public class BotTests 
    {
        [Fact]
        public async Task TestResponseToQuesion()
        {
            // Note: this test requires that SaveChangesAsync is made virtual in order to be able to create a mock.
            var memoryStorage = new MemoryStorage();
            var mockConversationState = new Mock<ConversationState>(memoryStorage)
            {
                CallBase = true,
            };

            var mockUserState = new Mock<UserState>(memoryStorage)
            {
                CallBase = true,
            };

            // You need to mock a dialog because most bots require a Dialog to instantiate it.
            // If yours doesn't you can likely skip this
            var mockRootDialog = SimpleMockFactory.CreateMockDialog<Dialog>(null, "mockRootDialog");
            var mockLogger = new Mock<ILogger<DispatchBot<Dialog>>>();

            // Act
            var sut = new DispatchBot<Dialog>(mockConversationState.Object, mockUserState.Object, mockRootDialog.Object, mockLogger.Object);
            var testAdapter = new TestAdapter();
            var testFlow = new TestFlow(testAdapter, sut);
            await testFlow
                    .Send("<Whatever you want to send>")
                    .AssertReply("<Whatever you expect the reply to be")
                    .StartTestAsync();
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章