1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:28:48 +00:00

test: add unit tests for ServiceCollectionExtensions

This commit is contained in:
Oliver Booth 2023-04-02 03:31:19 +01:00
parent ceaa254d7a
commit bf73ecce32
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
2 changed files with 62 additions and 0 deletions

View File

@ -14,6 +14,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0"/>
<PackageReference Include="Moq" Version="4.18.4"/>
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2"/>
@ -25,6 +26,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\X10D.Hosting\X10D.Hosting.csproj"/>
<ProjectReference Include="..\X10D\X10D.csproj"/>
</ItemGroup>

View File

@ -0,0 +1,60 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using X10D.Hosting.DependencyInjection;
namespace X10D.Tests.Hosting;
[TestClass]
public class ServiceCollectionTests
{
[TestMethod]
public void AddHostedSingleton_ShouldRegisterServiceAsSingletonAndAsHostedService()
{
var services = new ServiceCollection();
services.AddHostedSingleton<TestService>();
var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetService<TestService>();
var hostedService = serviceProvider.GetService<IHostedService>();
Assert.IsNotNull(service);
Assert.IsNotNull(hostedService);
Assert.IsInstanceOfType(service, typeof(TestService));
Assert.IsInstanceOfType(hostedService, typeof(TestService));
Assert.AreSame(service, hostedService);
}
[TestMethod]
public void AddHostedSingleton_ShouldRegisterServiceTypeAsSingletonAndAsHostedService()
{
var services = new ServiceCollection();
services.AddHostedSingleton(typeof(TestService));
var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetService<TestService>();
var hostedService = serviceProvider.GetService<IHostedService>();
Assert.IsNotNull(service);
Assert.IsNotNull(hostedService);
Assert.IsInstanceOfType(service, typeof(TestService));
Assert.IsInstanceOfType(hostedService, typeof(TestService));
Assert.AreSame(service, hostedService);
}
private sealed class TestService : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}