mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-23 00:18:47 +00:00
test: add unit tests for ServiceCollectionExtensions
This commit is contained in:
parent
ceaa254d7a
commit
bf73ecce32
@ -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>
|
||||
|
||||
|
60
X10D.Tests/src/Hosting/ServiceCollectionTests.cs
Normal file
60
X10D.Tests/src/Hosting/ServiceCollectionTests.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user