【问题标题】:Can we use test for Adaptive Card replies with TestAdaptor and TestFlow? BotFramework我们可以使用 TestAdaptor 和 TestFlow 对自适应卡回复进行测试吗?机器人框架
【发布时间】:2019-05-02 01:36:43
【问题描述】:

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

在此对话框中,选择软件时,将发送自适应卡。 从客户端看起来像这样。 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();
        }

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

我认为需要某种方式来访问发送给用户的 bot 的活动附件,但目前无法确定如何执行此操作。

谢谢!

【问题讨论】:

    标签: c# unit-testing botframework integration-testing


    【解决方案1】:

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

    【讨论】:

    • CheckAttachment 的返回类型不是void 是否有原因?
    • 不是真的,更新了帖子以返回类型 void。谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 2021-04-16
    • 2018-03-06
    • 2016-04-16
    • 2017-10-18
    • 1970-01-01
    • 2021-01-01
    • 2020-03-29
    相关资源
    最近更新 更多