gpt4 book ai didi

c# - 如何在 Bot Framework C# 中对包含附件或更多对象的对话框进行单元测试

转载 作者:行者123 更新时间:2023-11-30 17:30:40 24 4
gpt4 key购买 nike

机器人信息

  • SDK 平台:.NET
  • SDK 版本:3.14.0.7
  • 活跃 channel :网络
  • 部署环境:使用模拟器进行本地开发

问题描述

我们试图对存储在某个字典中的每个案例进行单元测试,当用户发送字符串并且测试必须用字符串回答时,它似乎工作正常。但是我们找不到任何关于如何测试其他类型对话框的文档,例如附件、按钮等。

我们希望制作一个字符串、对象的字典,其中字符串是我们向机器人询问的内容,而对象是字符串、附件、对话框。

代码示例

这是我们存储答案的方式:

 public static Dictionary<string, object> data = new Dictionary<string, object>{
{"Nuevo", "Que quieres crear?"},
{"Ayuda", "Ya te ayudas!"},
{"Adios", "Nos vemos!"},
{
"Coche",
new Attachment() {
ContentUrl = "https://media.ed.edmunds-media.com/subaru/impreza/2006/oem/2006_subaru_impreza_sedan_sti_fq_oem_1_500.jpg",
ContentType = "image/png",
Name = "Subaru_Impreza.png"
}
},
{
"Moto",
new Attachment() {
ContentUrl = "http://motos.honda.com.co/sites/default/files/motos/cb-1000-r-cc-menu-honda.png",
ContentType = "image/png",
Name = "moto.png"
}
},
{
"Perro",
new Attachment() {
ContentUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Taka_Shiba.jpg/1200px-Taka_Shiba.jpg",
ContentType = "image/png",
Name = "ShibaInu.png"
}
}
};

这就是机器人的工作原理和返回所有内容的方式,这至少对文本和附件是按预期工作的,但我们还没有对更多类型的消息这样做。

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;

var r = context.MakeMessage();

foreach (var item in data)
{
if (item.Key == activity.Text)
{
if (item.Value is Attachment)
{
r.Attachments = new List<Attachment>() { item.Value as Attachment };
}
if (item.Value is string)
{
r.Text = item.Value.ToString();
}

break;
}
}

// return our reply to the user
await context.PostAsync(r);

context.Wait(MessageReceivedAsync);
}

但是当我们要对其进行测试时,它只有在我们发送的是字符串而不是在模拟器中工作的 IMessageActivity 时才有效。

测试代码:

 [TestMethod]
public async Task Pregunta_respuesta_prueba()
{
foreach (var item in RootDialog.data)
{
var preg = item.Key;
var resp = item.Value;

if (item.Value is Attachment)
{
Attachment auxText = resp as Attachment;
resp = auxText.ContentUrl;
}

using (ShimsContext.Create())
{
// Arrange
var waitCalled = false;
object message = null;

var target = new RootDialog();

var activity = new Activity(ActivityTypes.Message)
{
Text = preg
};

var awaiter = new Microsoft.Bot.Builder.Internals.Fibers.Fakes.StubIAwaiter<IMessageActivity>()
{

IsCompletedGet = () => true,
GetResult = () => activity
};

var awaitable = new Microsoft.Bot.Builder.Dialogs.Fakes.StubIAwaitable<IMessageActivity>()
{
GetAwaiter = () => awaiter
};

var context = new Microsoft.Bot.Builder.Dialogs.Fakes.StubIDialogContext();

Microsoft.Bot.Builder.Dialogs.Fakes.ShimExtensions.PostAsyncIBotToUserStringStringCancellationToken = (user, s1, s2, token) =>
{
message = s1;
Console.WriteLine(message);
return Task.CompletedTask;
};

Microsoft.Bot.Builder.Dialogs.Fakes.ShimExtensions.WaitIDialogStackResumeAfterOfIMessageActivity = (stack, callback) =>
{
if (waitCalled) return;

waitCalled = true;

// The callback is what is being tested.
callback(context, awaitable);
};

// Act
await target.StartAsync(context);

// Assert
Assert.AreEqual(resp, message);
}
}
}

如果你检查这部分代码

Microsoft.Bot.Builder.Dialogs.Fakes.ShimExtensions.PostAsyncIBotToUserStringStringCancellationToken = (user, s1, s2, token) =>
{
message = s1;
Console.WriteLine(message);
return Task.CompletedTask;
};

```

它仅在机器人返回字符串时有效,我们甚至无法检查它是否是一个事件,发生这种情况是因为我们为测试创建的假上下文没有按预期工作。

我们伪造的 IDialogContext 在它是一个对象时似乎根本不起作用,但在它是一个字符串时它确实起作用。

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;

/// Here when the test is running, this context.MakeMessage is null, but when the bot
/// is working, it wors perfectly.
var r = context.MakeMessage();

foreach (var item in data)
{
if (item.Key == activity.Text)
{
if (item.Value is Attachment)
{
r.Attachments = new List<Attachment>() { item.Value as Attachment };
}
if (item.Value is string)
{
r.Text = item.Value.ToString();
}

break;
}
}

// return our reply to the user
await context.PostAsync(r);

context.Wait(MessageReceivedAsync);
}

复制步骤

要尝试这一点,您可以尝试使用附件进行测试,代码在 this 中。存储库。

最佳答案

除了使用 PostAsyncIBotToUserStringStringCancellationToken,您还可以使用 context.PostAsyncIMessageActivityCancellationToken 并且,在 RootDialog 的 MessageReceivedWithTextAsync 中使用事件回复而不只是字符串进行响应。

public async Task MessageReceivedWithTextAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;

string r = "";

foreach (var item in dataText)
{
if (item.Key == activity.Text)
{
r = item.Value;
break;
}
}

var reply = activity.CreateReply(r);
foreach (var item in dataAtt)
{
if (item.Key == activity.Text)
{
reply.Attachments.Add(item.Value);
reply.Text = "attachment";
break;
}
}

if ((string.IsNullOrWhiteSpace(r) || r == null) && reply.Attachments.Count == 0)
{
reply.Text = "No tengo respuesta para eso.";
}


// return our reply to the user
await context.PostAsync(reply);
}

以下是对测试方法的更改:

[TestMethod]
public async Task Bot_Test_Attachments()
{
foreach (var item in RootDialog.dataAtt)
{
var preg = item.Key;
var att = item.Value;

using (ShimsContext.Create())
{
var waitCalled = false;
IMessageActivity message = null;

var target = new RootDialog();

var activity = new Activity(ActivityTypes.Message)
{
Text = preg,
From = new ChannelAccount("id","name"),
Recipient = new ChannelAccount("recipid","recipname"),
Conversation = new ConversationAccount(false,"id","name")
};

var awaiter = new Microsoft.Bot.Builder.Internals.Fibers.Fakes.StubIAwaiter<IMessageActivity>()
{
IsCompletedGet = () => true,
GetResult = () => activity
};

var awaitable = new Microsoft.Bot.Builder.Dialogs.Fakes.StubIAwaitable<IMessageActivity>()
{
GetAwaiter = () => awaiter
};

var context = new Microsoft.Bot.Builder.Dialogs.Fakes.StubIDialogContext();

context.PostAsyncIMessageActivityCancellationToken = (messageActivity, token) => {
message = messageActivity;
return Task.CompletedTask;
};

Microsoft.Bot.Builder.Dialogs.Fakes.ShimExtensions.WaitIDialogStackResumeAfterOfIMessageActivity = (stack, callback) =>
{
if (waitCalled) return;
waitCalled = true;
callback(context, awaitable);
};
await target.MessageReceivedWithTextAsync(context, awaitable);


Assert.AreEqual(att, message.Attachments[0]);
}
}
}

关于c# - 如何在 Bot Framework C# 中对包含附件或更多对象的对话框进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49385302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com