I’ve previously written a quick post showing a unit test for v4 preview release. I thought I’d update it.
Goal
Unit Test a dialog we’ve written that asks for a user first and second name.
Steps for creating a unit Test
- Create a unit test project in your solution, I chose an MS Test project
- Using Nuget install Microsoft.Bot.Builder.Dialogs into the MS Test project
- Reference your Bot project
- Create a unit test, remember to make it
async Task
[TestMethod]
public async Task CreatingAGoodContact()
{
var convoState = new ConversationState(new MemoryStorage());
var adapter = new TestAdapter()
.Use(new AutoSaveStateMiddleware(convoState));
var dialogState = convoState.CreateProperty("dialogState");
var dialogs = new DialogSet(dialogState);
dialogs.Add(new CreateContactDialog(null));
await new TestFlow(adapter, async (turnContext, cancellationToken) =>
{
var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);
await dc.ContinueDialogAsync(cancellationToken);
if (!turnContext.Responded)
{
await dc.BeginDialogAsync("CreateContactDialog", null, cancellationToken);
}
})
.Send("Say something to start test")
.AssertReply("What is their first name?")
.Send("Jane")
.AssertReply("What is their last name?")
.Send("Tan")
.AssertReply($"I have created a contact called Jane Tan")
.StartTestAsync();
}