feat(sphistorianclient): add AddZbSpHistorianClient DI extension
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ZB.MOM.WW.SPHistorianClient;
|
||||
|
||||
namespace ZB.MOM.WW.SPHistorianClient.Tests;
|
||||
|
||||
public class DependencyInjectionTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task AddZbSpHistorianClient_resolves_client_and_options()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
var options = new HistorianClientOptions { Host = "localhost" };
|
||||
|
||||
services.AddZbSpHistorianClient(options);
|
||||
|
||||
// HistorianClient is IAsyncDisposable-only, so the container must be disposed
|
||||
// asynchronously (a synchronous `using` throws InvalidOperationException).
|
||||
await using var sp = services.BuildServiceProvider();
|
||||
Assert.Same(options, sp.GetRequiredService<HistorianClientOptions>());
|
||||
Assert.NotNull(sp.GetRequiredService<HistorianClient>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddZbSpHistorianClient_throws_when_host_missing()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
var options = new HistorianClientOptions { Host = "" };
|
||||
|
||||
Assert.Throws<ArgumentException>(() => services.AddZbSpHistorianClient(options));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddZbSpHistorianClient_throws_on_null_options()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
Assert.Throws<ArgumentNullException>(() => services.AddZbSpHistorianClient(null!));
|
||||
}
|
||||
}
|
||||
+1
@@ -6,6 +6,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" />
|
||||
<PackageReference Include="xunit" />
|
||||
|
||||
Reference in New Issue
Block a user