微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用TestFlow时如何将选项传递给对话框-Azure botframework v4-C#-单元测试

如何解决使用TestFlow时如何将选项传递给对话框-Azure botframework v4-C#-单元测试

使用Azure Bot框架v4-C#... xunit ...

我有以下单元测试,用于测试依次使用AdaptiveDialog的组件对话框。

作为对该对话框进行单元测试的一部分,我想将选项传递给对话框-传递给Dialog类的OnBeginDialogAsync重载的选项。

关于如何将myDialogoptions作为对话框的选项传递的任何想法?

谢谢 问候 阿萨杜

    public class ConfirmationDialog : ComponentDialog
    {
        public class Options
        {
            public string PromptTemplate { get; set; }
        }

        public ConfirmationDialog()
            : base("test")
        {
            
        }

        protected override Task<DialogTurnResult> OnBeginDialogAsync(DialogContext innerDc,object options,CancellationToken cancellationToken = default)
        {
            //
            // Avoiding using BotState here.  Instead ... use options...
            //    do something based on passed in options 
            //
        }
    }

        [Fact]
        public async Task TestMyComponentDialogThatUsesAdaptiveDialog()
        {
            //Arrange
            Setup();
            TestAdapter = (TestAdapter)new TestAdapter("my")
                                .UseStorage(memoryStorage)
                                .UseBotState(UserState,ConversationState)
                                .Use(Middlewares[0]);
            var dialogState = ConversationState.CreateProperty<DialogState>("dialogState");

            var dialogToTest = new ConfirmationDialog();
            var dialogManager = new DialogManager(dialogToTest);

            var myDialogoptions = new MyOptions { Name = "Jon Doe" };

            await new TestFlow(TestAdapter,async (turnContext,cancellationToken) =>
            {
                <<<<<<  How to pass in Dialog Options myDialogoptions to the dialog - need to access it within OnBeginDialogAsync >>>>>
                <<<<<<  of Dialog class override method OnBeginDialogAsync(DialogContext innerDc,CancellationToken cancellationToken = default)   >>>>>

                var result = await dialogManager.OnTurnAsync(turnContext,cancellationToken);
            })
            //Act
             .SendConversationUpdate()
            //Assert
             .AssertReply(activity =>
                 {
                     var resolvedActivity = activity.AsMessageActivity();
                     resolvedActivity.Text.Should().StartWith("Some Text");
                 },null,2100)
             .StartTestAsync();
        }

解决方法

您可以在source code中看到,对话框管理器没有将任何选项传递给其根对话框:

private async Task<DialogTurnResult> HandleBotOnTurnAsync(DialogContext dc,CancellationToken cancellationToken)
{
    DialogTurnResult turnResult;

    // the bot is running as a root bot. 
    if (dc.ActiveDialog == null)
    {
        // start root dialog
        turnResult = await dc.BeginDialogAsync(_rootDialogId,cancellationToken: cancellationToken).ConfigureAwait(false);
    }
    else
    {
        // Continue execution
        // - This will apply any queued up interruptions and execute the current/next step(s).
        turnResult = await dc.ContinueDialogAsync(cancellationToken).ConfigureAwait(false);

        if (turnResult.Status == DialogTurnStatus.Empty)
        {
            // restart root dialog
            turnResult = await dc.BeginDialogAsync(_rootDialogId,cancellationToken: cancellationToken).ConfigureAwait(false);
        }
    }

    await SendStateSnapshotTraceAsync(dc,"Bot State",cancellationToken).ConfigureAwait(false);

    return turnResult;
}

如果您要将选项传递给BeginDialogAsync,则应该自己调用或PromptAsync

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。