deetwo-id
deetwo-id

Reputation: 255

How to unit test a single conversational statement

i am working with bots and the Microsoft Bot Framework. I used the DispatchBot template to generate my bot. (https://learn.microsoft.com/de-de/azure/bot-service/bot-builder-tutorial-dispatch?view=azure-bot-service-4.0&tabs=cs)

For conversational testing, i want to create unit tests. Therefore i used this documentation to gather some informations (https://learn.microsoft.com/de-de/azure/bot-service/unit-test-bots?view=azure-bot-service-4.0&tabs=csharp)

The thing is that i dont want to test dialogs, but a single statement (a question and the right answer) How can i implement this?

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

Upvotes: 0

Views: 397

Answers (1)

mdrichardson
mdrichardson

Reputation: 7241

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

Upvotes: 3

Related Questions