1
votes

I'm currently attempting to write unit tests for my bot, however, the test always fail when attempting to get a response. I've created a mock test which inherits DialogTestBase.

[TestClass]
public class Tests : DialogTestBase
{
    [TestMethod]
    public async Task TestDialogTest()
    {
        await TestDialogFlow(new TestDialog());
    }

    private async Task TestDialogFlow(IDialog<object> echoDialog)
    {
        // arrange
        var toBot = DialogTestBase.MakeTestMessage();
        toBot.From.Id = Guid.NewGuid().ToString();
        toBot.Text = "Hi";

        Func<IDialog<object>> MakeRoot = () => echoDialog;

        using (new FiberTestBase.ResolveMoqAssembly(echoDialog))
        using (var container = Build(Options.MockConnectorFactory | Options.ScopedQueue, echoDialog))
        {
            IMessageActivity toUser = await GetResponse(container, MakeRoot, toBot);

            Assert.IsTrue(toUser.Text.StartsWith("Hello"));
        }
    }

    private async Task<IMessageActivity> GetResponse(IContainer container, Func<IDialog<object>> makeRoot, IMessageActivity toBot)
    {
        using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
        {
            DialogModule_MakeRoot.Register(scope, makeRoot);

            // act: sending the message
            await Conversation.SendAsync(toBot, makeRoot);
            return scope.Resolve<Queue<IMessageActivity>>().Dequeue();
        }
    }
}

The dialog i'm testing is:

[Serializable]
public class TestDialog : IDialog
{
    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(ProcessMessage);
    }

    public async Task ProcessMessage(IDialogContext context, IAwaitable<IMessageActivity> argument)
    {
        await context.PostAsync("Hello. I'm a bot");
        await context.PostAsync("How can I help?");

        context.Wait(ProcessRequest);
    }

    public async Task ProcessRequest(IDialogContext context, IAwaitable<IMessageActivity> argument)
    {
        var request = await argument;

        await context.PostAsync($"You asked the following question: {request}");

        context.Done(true);
    }
}

When I run the test I get the following error:

Autofac.Core.DependencyResolutionException: An exception was thrown while executing a resolve operation. See the InnerException for details. ---> Invalid URI: The format of the URI could not be determined. (See inner exception for details.) --->System.UriFormatException: Invalid URI: The format of the URI could not be determined.

The problem happens in the GetResponse method when we send the request. Any help would be greatly appreciated.

1

1 Answers

1
votes

The GetResponse method should actually call SendAsync using scope and toBot as parameters:

await Conversation.SendAsync(toBot, makeRoot);

I tried the code you've shared and after making this change, the test passes.