Microsoft Bot Framework - 单元测试失败

时间:2017-05-01 22:25:17

标签: c# unit-testing botframework

我目前正在尝试为我的机器人编写单元测试,但是,在尝试获得响应时测试总是失败。我创建了一个继承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();
        }
    }
}

我正在测试的对话是:

[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);
    }
}

当我运行测试时,我收到以下错误:

  

Autofac.Core.DependencyResolutionException:执行解析操作时抛出异常。有关详细信息,请参阅InnerException。 ---&GT;无效的URI:无法确定URI的格式。 (详见内部异常。)---&gt; System.UriFormatException:无效的URI:无法确定URI的格式。

当我们发送请求时,问题发生在GetResponse方法中。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

GetResponse方法实际上应使用SendAsyncscope作为参数调用toBot

await Conversation.SendAsync(toBot, makeRoot);

我尝试了您共享的代码,在进行此更改后,测试通过了。