Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...