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

How to unit test a single conversational statement

我正在使用机器人和 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)

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

在这里您可以看到我的 Dispatchbot.cs 文件的开头,其中发生了神奇的事情(搜索正确的知识库等)

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。根据你可以在那里找到的测试代码,你可能想要这样的东西:

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();
        }
    }
}