botbuilder-dotnet/tests/Microsoft.Bot.Builder.Trans.../CoreTests.cs

71 строка
2.6 KiB
C#
Исходник Обычный вид История

2018-06-13 20:52:37 +03:00
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
2018-07-03 19:02:16 +03:00
using System.Threading;
2018-05-21 23:39:12 +03:00
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Adapters;
2018-06-29 01:01:05 +03:00
using Microsoft.Bot.Builder.Tests;
2018-07-17 06:09:35 +03:00
using Microsoft.Bot.Schema;
using Xunit;
2018-05-21 21:04:53 +03:00
namespace Microsoft.Bot.Builder.Transcripts.Tests
{
public class CoreTests
{
public static readonly string ClassName = "CoreTests";
2018-05-21 23:39:12 +03:00
[Fact]
2018-05-21 23:39:12 +03:00
public async Task BotAdapted_Bracketing()
2018-05-21 21:04:53 +03:00
{
var testName = "BotAdapted_Bracketing";
var activities = TranscriptUtilities.GetActivitiesFromFile(ClassName, testName);
2018-05-21 23:39:12 +03:00
TestAdapter adapter = new TestAdapter(TestAdapter.CreateConversation(testName))
2018-07-17 06:09:35 +03:00
.Use(new BeforeAfterMiddleware());
adapter.OnTurnError = async (context, exception) =>
{
await context.SendActivityAsync($"Caught: {exception.Message}");
return;
};
2018-05-21 23:39:12 +03:00
2018-07-25 00:16:50 +03:00
var flow = new TestFlow(adapter, async (context, cancellationToken) =>
2018-07-17 06:09:35 +03:00
{
switch (context.Activity.Type)
2018-05-21 23:39:12 +03:00
{
2018-07-17 06:09:35 +03:00
case ActivityTypes.Message:
{
var userMessage = context.Activity.AsMessageActivity()?.Text;
switch (userMessage)
{
case "use middleware":
await context.SendActivityAsync("using middleware");
break;
case "catch exception":
await context.SendActivityAsync("generating exception");
throw new Exception("exception to catch");
}
}
2018-07-17 06:09:35 +03:00
break;
default:
await context.SendActivityAsync(context.Activity.Type);
2018-05-21 23:39:12 +03:00
break;
}
});
await flow.Test(activities).StartTestAsync();
2018-05-21 23:39:12 +03:00
}
2018-05-21 21:04:53 +03:00
2018-05-21 23:39:12 +03:00
public class BeforeAfterMiddleware : IMiddleware
{
2018-08-23 04:43:11 +03:00
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default(CancellationToken))
2018-05-21 23:39:12 +03:00
{
2018-08-23 20:16:19 +03:00
await turnContext.SendActivityAsync("before message");
2018-07-03 19:02:16 +03:00
await next(cancellationToken);
2018-08-23 20:16:19 +03:00
await turnContext.SendActivityAsync("after message");
2018-05-21 23:39:12 +03:00
}
}
2018-05-21 21:04:53 +03:00
}
}