How to unit test Microsoft bot dialog with prompt

I am using Microsoft Bot Framework and I am 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 returned IA result comes in as true it responds with "Awesome", and if its false its "Sorry".

PromptDialog is a class with a static method Validate

I have checked modules dialogs before successfully using moq to mock the IMessageActivity and IDialogContext being passed to the dialog. It seems more complicated because I want to mock the state of the dialog.

Still:

    [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 on how to do this?

+3


source to share


1 answer


A good source for understanding how using unit tests dialog boxes is the Microsoft.Bot.Sample.Tests project from the BotBuilder GitHub repository .

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



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

+3


source







All Articles