0

I have a MSFT Bot Framework application for which I am trying to write unit tests. I have single call/response tests working fine, but anything that requires continuing a dialog does not work. The ActiveDialog property in the DialogContext that get's created on every turn is always null and the Stack property is always empty. I was sort of following this blog post example. What am I missing that allows the bot to maintain it's state between turns?

One-Time Setup

        protected virtual void TestFixtureSetup()
        {
            var environmentName = "development";
            var builder = new ConfigurationBuilder();
            var configuration = builder.Build();

            _connectedServices = new BotServices(_botConfig, configuration, environmentName);
            _testAdapter = new TestAdapter();
            _testAdapter.Use(new AutoSaveStateMiddleware());
        }

Per-Test Setup

        protected virtual void TestSetup()
        {
            var memStore = new MemoryStorage();
            var userState = new UserState(memStore);
            var conversationState = new ConversationState(memStore);
            var dialogState = conversationState.CreateProperty<DialogState>("dialogState");
            _dialogSet = new DialogSet(dialogState);
            _dialogSet.Add(new MainDialog(_connectedServices, conversationState, userState, new TestTelemetryClient()));

            _testFlow = new TestFlow(_testAdapter, async (turnContext, cancellationToken) =>
            {
                var dc = await _dialogSet.CreateContextAsync(turnContext, cancellationToken);
                await dc.ContinueDialogAsync();
                if (!turnContext.Responded)
                {
                    await dc.BeginDialogAsync(nameof(MainDialog));
                }
            });
        }

Test

        public async Task MenuTestMethod(string subMenuOption, string verificationString)
        {
            var firstMenu = Responses.BuildFirstMenu(null, null);
            var secondMenu = Responses.BuildSecondMenu(null, null);
            await _testFlow
                .Send("go to first menu")
                .AssertReply((activity) =>
                {
                    Assert.AreEqual(firstMenu.Attachments[0].Content, activity.AsMessageActivity().Attachments[0].Content);
                })
                .Send("go to second menu")
                .AssertReply((activity) =>
                {
                    Assert.AreEqual(secondMenu.Attachments[0].Content, activity.AsMessageActivity().Attachments[0].Content);
                })
                .Send(subMenuOption)
                .AssertReply((activity) =>
                {
                    Assert.IsTrue(activity.AsMessageActivity().Text.Contains(verificationString));
                })
                .StartTestAsync();
        }
DevNoob
  • 536
  • 6
  • 22
  • I can help you with this. Can you edit in the code for the dialog that you're testing? – mdrichardson Aug 06 '19 at 16:12
  • Yeah...I've read through all of your provided code and I don't believe your unit test is the issue. It would be great if you can link to your repo. If not, please include any code for your Dialog, `OnTurn`, `OnMessage`, `OnDialog`, and anything else you think might be relevant. – mdrichardson Aug 06 '19 at 16:29
  • Do you still need help with this? If so, please include your code as requested above. [You can see here](https://github.com/microsoft/botbuilder-dotnet/blob/master/tests/Microsoft.Bot.Builder.Dialogs.Tests/WaterfallTests.cs) that unit tests with a continued dialog should work. – mdrichardson Aug 12 '19 at 15:17
  • @mdrichardson-MSFT I found that it seems to work for me as long as I don't start from the `MainDialog`. I'm sure it is something we are doing wrong in there. Unfortunately, I've run into other problems [LUIS/dispatcher related](https://stackoverflow.com/questions/57482652/luis-app-fails-to-train-app-training-failed-l-general-retrain) that are more pressing. Thanks for looking into it! – DevNoob Aug 13 '19 at 17:41
  • Are you wanting to work on this issue again? Feel free to send your code via Skype/Teams if you'd like. I'll leave the support ticket open, unless you're no longer interested in solving this "soon". – mdrichardson Aug 27 '19 at 18:31

0 Answers0