如何对带有提示的Microsoft Bot对话框进行单元测试 [英] How to unit test a Microsoft bot dialog with a prompt

查看:62
本文介绍了如何对带有提示的Microsoft Bot对话框进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft Bot Framework,并且试图对一个对话框进行单元测试(单独):

public class MyDialog : IDialog
{
    public async Task StartAsync(IDialogContext context)
    {
        PromptDialog.Confirm(context, MessageReceived, "Are you sure?", "Sorry what was that?");
    }

    private async Task MessageReceived(IDialogContext context, IAwaitable<bool> result)
    {
        bool isSure = await result;
        string response = isSure ? "Awesome" : "Sorry";
        IMessageActivity messageActivity = context.MakeMessage();
        messageActivity.Text = response;
        await context.PostAsync(messageActivity);
        context.Done<object>(null);
    }
}

我想证明,如果IAwaitable结果为true,则返回"Awesome",如果为false,则返回"Sorry".

PromptDialog是带有静态方法的类

有人对此有任何建议或想法吗?

EchoBotTests 是最容易开始的.它显示了如何使用模拟的连接器工厂向机器人发送消息并获得对它的响应.

关键是要从 DialogTestBase ,它提供了非常有用的帮助程序方法.

I'm using Microsofts Bot Framework and I'm trying to unit test (in isolation) a Dialog that I have:

public class MyDialog : IDialog
{
    public async Task StartAsync(IDialogContext context)
    {
        PromptDialog.Confirm(context, MessageReceived, "Are you sure?", "Sorry what was that?");
    }

    private async Task MessageReceived(IDialogContext context, IAwaitable<bool> result)
    {
        bool isSure = await result;
        string response = isSure ? "Awesome" : "Sorry";
        IMessageActivity messageActivity = context.MakeMessage();
        messageActivity.Text = response;
        await context.PostAsync(messageActivity);
        context.Done<object>(null);
    }
}

I want to prove that if the IAwaitable result comes in as true, it replies with "Awesome" and if its false its "Sorry".

PromptDialog is a class with a static method Confirm

I have unit tested dialogs before successfully using moq to mock the IMessageActivity and IDialogContext that is passed into the dialog. This feels more complicated because I want to mock a state of the dialog.

So Far:

    [TestFixture]
public class Tests
{
    private Mock<IDialogContext> _dialogContext;
    private Mock<IMessageActivity> _messageActivity;
    private MyDialog _myDialog;

    [SetUp]
    public void Setup()
    {
        _dialogContext = new Mock<IDialogContext>();
        _messageActivity = new Mock<IMessageActivity>();
        _messageActivity.SetupAllProperties();
        _dialogContext.SetupSequence(x => x.MakeMessage())
            .Returns(_messageActivity.Object);

        _myDialog = new MyDialog();
    }

    [Test]
    public void GivenTrue_WhenIConfirmPrompt_ThenAwesome()
    {
        _myDialog
            .StartAsync(_dialogContext.Object)
            .Wait(CancellationToken.None);

        Assert.That(_messageActivity.Object.Text, Is.EqualTo("Awesome"));
    }

    [Test]
    public void GivenTrue_WhenIRejectPrompt_ThenSorry()
    {
        _myDialog
            .StartAsync(_dialogContext.Object)
            .Wait(CancellationToken.None);

        Assert.That(_messageActivity.Object.Text, Is.EqualTo("Sorry"));
    }
}

Does anyone have any suggestions or ideas how to do this?

解决方案

A good source to understand how to unit tests dialogs is the Microsoft.Bot.Sample.Tests project from the BotBuilder GitHub repository.

There you will find the way the Bot Framework team is doing unit tests. The EchoBotTests are the easiest one to start with. It shows how to send a message to the bot and get the response to it, using a mocked connector factory.

The key is to inherit from DialogTestBase which provides very useful helper methods.

这篇关于如何对带有提示的Microsoft Bot对话框进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆