我尝试从对话框中单元测试方法,一旦用户说出了LUIS已知的关键字,就会调用此方法。这里是代码流,
"我想重置密码",密码是与CustomerService意图相关联的实体。
[LuisIntent("CustomerService")]
public async Task customerServiceRequest(IDialogContext context, LuisResult result)
{
try
{
var entity = result.Entities.SingleOrDefault(c => c.Type == "ServiceKeyword" && customerSupportKeywords.Contains(c.Entity.ToLower()));
if (!ReferenceEquals(entity, null))
{
switch (entity.Entity.ToLower())
{
case "password":
PromptDialog.Choice(context, AfterUserHasChosenAsync, new string[] { "SMS", "Email" }, "¿How would you like reset your password?", attempts: 100);
break;
default:
break;
}
}
}
catch (exception ex)
{
}
}
。当我选择电子邮件作为通过重置时,机器人要求我输入我的电子邮件,此输入将根据正则表达式进行验证。
private async Task AfterUserHasChosenAsync(IDialogContext context, IAwaitable<string> result)
{
try
{
string userChoice = await result;
switch (userChoice)
{
case "Email":
context.Call(AskForEmailToVerifyDialog(), AskForEmailToVerifyDialogStep1);
break;
case "SMS":
context.Call(AskForCellNumberToVerifyDialog(), AskForCellNumberToVerifyDialogStep1);
break;
default:
await context.PostAsync("Something was wrong");
context.Done(true);
break;
}
}
catch (TooManyAttemptsException)
{
await context.PostAsync(AnswerNotFoundMsg);
context.Done(true);
}
catch (Exception ex)
{
}
}
。
private IDialog<ContactMedioObject> AskForEmailToVerifyDialog()
{
try
{
return Chain.From(() => new PromptDialog.PromptString("Please enter your current email", "", 1).
ContinueWith<string, ContactMedioObject>(async (context, result) =>
{
var email = await result;
return Chain.Return(new ContactMedioObject() { Email = email });
}));
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
private async Task AskForEmailToVerifyDialogStep1(IDialogContext context, IAwaitable<ContactMedioObject> result)
{
try
{
var Cell = await result;
if (!Cell.isValidEmail)
{
PromptDialog.Choice(context, OnResumeConfirmEmail, new string[] { "Yes", "No" }, "Would you like to try again?", attempts: 9999);
}
else
{
await context.PostAsync($".................");
context.Done(true);
}
}
catch (Exception ex)
{
}
}
我已经使用botbuilder存储库JMS serialization中的相同sintax对customerServiceRequest进行了单元测试
但是我无法以同样的方式模拟AfterUserHasChosenAsync和AskForEmailToVerifyDialogStep1,任何想法如何测试这两种方法,考虑到AskForEmailToVerifyDialogStep1返回一个promptdialog,如果它是一个糟糕的电子邮件地址和PostAsync,如果一切正常。?< / p>