Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

c# - How to unit test a Microsoft bot dialog with a prompt

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...