我们可以使用 TestAdaptor 和 TestFlow 对自适应卡片回复进行测试吗?机器人框架

Can we use test for Adaptive Card replies with TestAdaptor and TestFlow? BotFramework

已经在 bot 的企业模板 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();
        }

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

我认为需要一些方法来访问发送给用户的机器人的 activity 附件,但现在无法确定如何完成此操作。

谢谢!

所以经过一番探索,找到了一种方法来解决这个问题,这个函数是 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();
}