c# - 我们可以使用 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();
}
关于如何验证自适应卡的输出的任何建议都会很棒。
我认为需要某种方式来访问从机器人发送给用户的活动附件,在确定如何立即完成此操作时遇到了一些麻烦。
谢谢!
解决方案
因此,经过一番探索,找到了一种方法来解决这个问题,这个函数是 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();
}
推荐阅读
- json - 为什么在 Spring 中使用 RestController 返回 JSON 4MB 时会出现性能问题
- ios - 如何在 ionic 中与其他应用程序共享文件
- docker - 在内容更改时重新加载 nginx 容器
- node.js - 等待所有子进程完成以继续
- typescript - 字符串类型的基元因 T extends null 而失败...?
- swing - 使 JavaFX 按钮在视觉上看起来像是在右键单击时被单击
- asp.net-mvc - ASP.NET Core MVC - 特定于控制器的复杂对象
- python - Matplotlib sharex 对具有不同 x 值的数据?
- sql-server - 使用不存在的案例语句作为 Where 子句的一部分
- python - 从 Skimage 高程图生成 3D 表面图(2D numpy.ndarray)