Unit Test Flow in Botframework V4

In theory unit testing in Botframework V4 should easy enough as it’s based on .net core. But, as with many things, it might not be that obvious. So here is a quick snippet showing a test for a Waterfall flow;

[TestMethod]
public async Task Waterfall()
{
    var convoState = new ConversationState<Dictionary>(new MemoryStorage());

    var adapter = new TestAdapter()
        .Use(convoState);

    //var dialogState = convoState.CreateProperty("dialogState");
    var dialogs = new DialogSet();
    dialogs.Add("textPrompt", new TextPrompt());

    dialogs.Add("test",
        new WaterfallStep[]
        {
            async (dc, args, next) =>
            {
                    await dc.Context.SendActivity("Welcome! We need to ask a few questions to get started.");
                    await dc.Prompt("textPrompt", "What's your name?");
            },
            async (dc, args, next) =>
            {
                dc.ActiveDialog.State["name"] = args["Value"];
                await dc.Context.SendActivity($"Thanks {args["Value"]}!");
                await dc.End();
            }
        }
    );

    await new TestFlow(adapter, async (turnContext) =>
    {
        var state = turnContext.GetConversationState<Dictionary>();
        var dc = dialogs.CreateContext(turnContext, state);
        await dc.Continue();
        if (!turnContext.Responded)
        {
            await dc.Begin("test");
        }
    })
    .Send("Hello")
    .AssertReply("Welcome! We need to ask a few questions to get started.")
    .AssertReply("What's your name?")
    .Send("Paul")
    .AssertReply("Thanks Paul!")
    .StartTest();
}

A couple of points to note, you have to send a message to start the flow, and make sure you call turnContext.GetConversationState rather than using a local version of the state.

Edit – you may want to also look at Automate Bot tests with saved transcript files , Testing using ConfigurationManager with .net core and How to write a unit test for Bot Framework v4

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s