首页 > 解决方案 > 我们可以使用 TestAdaptor 和 TestFlow 对自适应卡回复进行测试吗?机器人框架

问题描述

一直在机器人的企业模板 4.2.2 中编写一些测试,到目前为止,它对于基于文本的响应非常有效。但是,当流程涉及自适应卡片时,是否有办法访问附件以确保一切按预期工作?

In this dialog, when software is selected, an adaptive card is sent back. 从客户端看起来像这样。 https://imgur.com/a/aEDwFYl

[TestMethod]
        public async Task TestSoftwareIssue()
        {
            string resp = "What sort of issue do you have?\n\n" +
                            "   1. Computer\n" +
                            "   2. Software\n" +
                            "   3. Insuffient Permissions for Access\n" +
                            "   4. Account expired\n" +
                            "   5. Other";
            await GetTestFlow()
                .Send(GeneralUtterances.GeneralIssue)
                .AssertReply(resp)
                .Send("software")
                // Check attachment somehow?
                .AssertReply("")
                .StartTestAsync();
        }

关于如何验证自适应卡的输出的任何建议都会很棒。

我认为需要某种方式来访问从机器人发送给用户的活动附件,在确定如何立即完成此操作时遇到了一些麻烦。

谢谢!

标签: c#unit-testingbotframeworkintegration-testing

解决方案


因此,经过一番探索,找到了一种方法来解决这个问题,这个函数是 TestFlow 的一部分。


/// <param name="validateActivity">A validation method to apply to an activity from the bot.
/// This activity should throw an exception if validation fails.</param>

public TestFlow AssertReply(Action<IActivity> validateActivity, [CallerMemberName] string description = null, uint timeout = 3000)

然后我们可以创建自己的验证函数来处理断言。

public void CheckAttachment(IMessageActivity messageActivity)
{
    // Check if content is the same
    var messageAttachment = messageActivity.Attachments.First();
    // Example attachment
    var adaptiveCardJson = File.ReadAllText(@".\Resources\TicketForm.json");

    var expected = new Attachment()
    {
        ContentType = "application/vnd.microsoft.card.adaptive",
        Content = JsonConvert.DeserializeObject(adaptiveCardJson),
    };

    Assert.AreEqual(messageAttachment.Content.ToString(), expected.Content.ToString());
}

[TestMethod] 然后可以像这样工作。

[TestMethod]
public async Task TestSoftwareIssue()
{
    await GetTestFlow()
        .Send(GeneralUtterances.GeneralIssue)
        .AssertReply("Some Response")
        .Send("Some Choice")
        // .AssertReply("")
        .AssertReply(activity => CheckAttachment(activity.AsMessageActivity()))
        .StartTestAsync();
}

推荐阅读