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;

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

    var adapter = new TestAdapter()

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

        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");
    .AssertReply("Welcome! We need to ask a few questions to get started.")
    .AssertReply("What's your name?")
    .AssertReply("Thanks Paul!")

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.


How to Test async and await

This is one of those, “please stop forgetting this dummy” posts aimed at myself. When you want to test an asynchronous method implemented via the Task-based Asynchronous Pattern (TAP) don’t forget to change the default test method;



public void TestAsyncCall() { … }



public async Task TestAsyncCall() { … }