Initial set of unit tests for ASP.NET Core Integration

This introduces the first set of unit tests for the ASP.NET Core integration library. The tests currently cover scenarios related to the top level integration extension methods `AddBot<TBot>` and `UseBotFramework`.
This commit is contained in:
Drew Marsh 2018-05-04 11:05:37 -07:00
Родитель ae49cfbc3b
Коммит fa3c40e6ef
4 изменённых файлов: 250 добавлений и 0 удалений

Просмотреть файл

@ -98,6 +98,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Builder.Dialo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Samples.Ai.Luis.Dispatch", "samples\Microsoft.Bot.Samples.Ai.Luis.Dispatch\Microsoft.Bot.Samples.Ai.Luis.Dispatch.csproj", "{91178F5C-303C-43B8-9284-84125FB07FFF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Integration", "Integration", "{0A0E26B0-7A46-4F1A-8BFE-9A763FDF6CF8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Core.Tests", "tests\integration\AspNet.Core.Tests\AspNet.Core.Tests.csproj", "{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug - NuGet Packages|Any CPU = Debug - NuGet Packages|Any CPU
@ -418,6 +422,14 @@ Global
{91178F5C-303C-43B8-9284-84125FB07FFF}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{91178F5C-303C-43B8-9284-84125FB07FFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{91178F5C-303C-43B8-9284-84125FB07FFF}.Release|Any CPU.Build.0 = Release|Any CPU
{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD}.Debug - NuGet Packages|Any CPU.ActiveCfg = Debug|Any CPU
{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD}.Debug - NuGet Packages|Any CPU.Build.0 = Debug|Any CPU
{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD}.Documentation|Any CPU.ActiveCfg = Debug|Any CPU
{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -468,6 +480,8 @@ Global
{B71A123F-09FE-45D7-9644-4EE3E1E221AE} = {3ADFB27A-95FA-4330-B211-1D66A29A17AB}
{2F77CA1D-E6F0-4DEA-96BB-8A039F4D0FF8} = {AD743B78-D61F-4FBF-B620-FA83CE599A50}
{91178F5C-303C-43B8-9284-84125FB07FFF} = {3ADFB27A-95FA-4330-B211-1D66A29A17AB}
{0A0E26B0-7A46-4F1A-8BFE-9A763FDF6CF8} = {AD743B78-D61F-4FBF-B620-FA83CE599A50}
{8A81D4F6-4E96-472B-9AE8-E1CAA5CC76FD} = {0A0E26B0-7A46-4F1A-8BFE-9A763FDF6CF8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7173C9F3-A7F9-496E-9078-9156E35D6E16}

Просмотреть файл

@ -0,0 +1,136 @@
using System;
using FluentAssertions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
namespace AspNet.Core
{
public class ApplicationBuilderExtensionsTests
{
public class UseBotFramework
{
[Fact]
public void NullApplicationBuilderThrows()
{
var nullApplicationBuilder = default(IApplicationBuilder);
var action = new Action(() => nullApplicationBuilder.UseBotFramework());
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("applicationBuilder");
}
[Fact]
public void WithoutPathConfigurationCallback()
{
var botFrameworkOptionsMock = new Mock<IOptions<BotFrameworkOptions>>();
botFrameworkOptionsMock.Setup(o => o.Value)
.Returns(new BotFrameworkOptions());
var serviceProviderMock = new Mock<IServiceProvider>();
serviceProviderMock.Setup(sp => sp.GetService(typeof(IOptions<BotFrameworkOptions>)))
.Returns(botFrameworkOptionsMock.Object);
var applicationBuilderMock = new Mock<IApplicationBuilder>();
applicationBuilderMock.Setup(ab => ab.ApplicationServices)
.Returns(serviceProviderMock.Object);
var mappedApplicationBuilderMock = new Mock<IApplicationBuilder>();
applicationBuilderMock.Setup(ab => ab.New())
.Returns(mappedApplicationBuilderMock.Object);
applicationBuilderMock.Object.UseBotFramework();
applicationBuilderMock.Verify(ab => ab.New(), Times.Once());
mappedApplicationBuilderMock.Verify(mab => mab.Build(), Times.Once());
mappedApplicationBuilderMock.Verify(ab => ab.Use(It.IsAny<Func<RequestDelegate, RequestDelegate>>()), Times.Once());
}
[Fact]
public void WithExplicitNullPathConfigurationCallback()
{
var applicationBuilderMock = new Mock<IApplicationBuilder>();
var action = new Action(() => applicationBuilderMock.Object.UseBotFramework(null));
action.Should().Throw<ArgumentNullException>();
}
[Fact]
public void WithPathConfigurationCallback()
{
var botFrameworkOptionsMock = new Mock<IOptions<BotFrameworkOptions>>();
botFrameworkOptionsMock.Setup(o => o.Value)
.Returns(new BotFrameworkOptions());
var serviceProviderMock = new Mock<IServiceProvider>();
serviceProviderMock.Setup(sp => sp.GetService(typeof(IOptions<BotFrameworkOptions>)))
.Returns(botFrameworkOptionsMock.Object);
var mappedApplicationBuilderMock = new Mock<IApplicationBuilder>();
var applicationBuilderMock = new Mock<IApplicationBuilder>();
applicationBuilderMock.Setup(ab => ab.ApplicationServices)
.Returns(serviceProviderMock.Object);
applicationBuilderMock.Setup(ab => ab.New())
.Returns(mappedApplicationBuilderMock.Object);
applicationBuilderMock.Object.UseBotFramework(paths =>
{
paths.Should().NotBeNull();
});
applicationBuilderMock.Verify(ab => ab.New(), Times.Once());
mappedApplicationBuilderMock.Verify(mab => mab.Build(), Times.Once());
mappedApplicationBuilderMock.Verify(mab => mab.Use(It.IsAny<Func<RequestDelegate, RequestDelegate>>()), Times.Once());
}
[Fact]
public void WhenEnableProactiveTrueShouldMapMultipleHandlers()
{
var botFrameworkOptionsMock = new Mock<IOptions<BotFrameworkOptions>>();
botFrameworkOptionsMock.Setup(o => o.Value)
.Returns(new BotFrameworkOptions
{
EnableProactiveMessages = true
});
var serviceProviderMock = new Mock<IServiceProvider>();
serviceProviderMock.Setup(sp => sp.GetService(typeof(IOptions<BotFrameworkOptions>)))
.Returns(botFrameworkOptionsMock.Object);
var applicationBuilderMock = new Mock<IApplicationBuilder>();
applicationBuilderMock.Setup(ab => ab.ApplicationServices)
.Returns(serviceProviderMock.Object);
var mappedApplicationBlocks = new[]
{
new Mock<IApplicationBuilder>(),
new Mock<IApplicationBuilder>()
};
var rootApplicationBuilderNewCallCount = 0;
applicationBuilderMock.Setup(ab => ab.New())
.Returns(() => mappedApplicationBlocks[rootApplicationBuilderNewCallCount++].Object);
applicationBuilderMock.Object.UseBotFramework(paths =>
{
paths.Should().NotBeNull();
});
applicationBuilderMock.Verify(ab => ab.New(), Times.Exactly(2));
foreach (var mappedApplicationBlock in mappedApplicationBlocks)
{
mappedApplicationBlock.Verify(mab => mab.Build(), Times.Once());
mappedApplicationBlock.Verify(mab => mab.Use(It.IsAny<Func<RequestDelegate, RequestDelegate>>()), Times.Once());
}
}
}
}
}

Просмотреть файл

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="xunit" Version="2.4.0-beta.1.build3958" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0-beta.1.build3958" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\libraries\integration\Microsoft.Bot.Builder.Integration.AspNet.Core\Microsoft.Bot.Builder.Integration.AspNet.Core.csproj" />
<ProjectReference Include="..\..\..\libraries\Microsoft.Bot.Builder.Core\Microsoft.Bot.Builder.Core.csproj" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,77 @@
using System;
using System.Threading.Tasks;
using Microsoft.Bot;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using FluentAssertions;
using Moq;
using Xunit;
namespace AspNet.Core
{
public class ServiceCollectionExtensionsTests
{
public class AddBot
{
[Fact]
public void NullServiceCollectionThrows()
{
var nullServiceCollection = default(IServiceCollection);
var action = new Action(() => nullServiceCollection.AddBot<TestBot>());
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("services");
}
[Fact]
public void WithoutConfigurationCallback()
{
var serviceCollectionMock = new Mock<IServiceCollection>();
serviceCollectionMock.Object.AddBot<TestBot>();
serviceCollectionMock.Verify(sc => sc.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(IBot) && sd.ImplementationType == typeof(TestBot) && sd.Lifetime == ServiceLifetime.Transient)));
serviceCollectionMock.Verify(sc => sc.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(BotFrameworkAdapter) && sd.Lifetime == ServiceLifetime.Singleton)));
serviceCollectionMock.Verify(sc => sc.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(IConfigureOptions<BotFrameworkOptions>))), Times.Never());
}
[Fact]
public void WithExplicitNullConfigurationCallback()
{
var serviceCollectionMock = new Mock<IServiceCollection>();
serviceCollectionMock.Object.AddBot<TestBot>(null);
serviceCollectionMock.Verify(sc => sc.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(IBot) && sd.ImplementationType == typeof(TestBot) && sd.Lifetime == ServiceLifetime.Transient)));
serviceCollectionMock.Verify(sc => sc.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(BotFrameworkAdapter) && sd.Lifetime == ServiceLifetime.Singleton)));
serviceCollectionMock.Verify(sc => sc.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(IConfigureOptions<BotFrameworkOptions>))), Times.Never());
}
[Fact]
public void WithConfigurationCallback()
{
var serviceCollectionMock = new Mock<IServiceCollection>();
serviceCollectionMock.Object.AddBot<TestBot>(options =>
{
options.Should().NotBeNull();
});
serviceCollectionMock.Verify(sc => sc.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(IBot) && sd.ImplementationType == typeof(TestBot) && sd.Lifetime == ServiceLifetime.Transient)));
serviceCollectionMock.Verify(sc => sc.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(BotFrameworkAdapter) && sd.Lifetime == ServiceLifetime.Singleton)));
serviceCollectionMock.Verify(sc => sc.Add(It.Is<ServiceDescriptor>(sd => sd.ServiceType == typeof(IConfigureOptions<BotFrameworkOptions>))), Times.Once());
}
}
internal sealed class TestBot : IBot
{
public Task OnTurn(ITurnContext turnContext)
{
throw new NotImplementedException();
}
}
}
}