Client web socket resolver (#47)

This commit is contained in:
2023-05-14 23:19:32 +02:00
committed by GitHub
parent de08c0e3b0
commit 2ed3de439e
11 changed files with 120 additions and 27 deletions
@@ -3,7 +3,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Slim;
using System;
using System.DependencyInjection.Http;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
@@ -2,7 +2,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Http;
using System.Net.Http;
namespace SystemExtensions.DependencyInjection.Tests.Http;
@@ -7,12 +7,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="FluentAssertions" Version="6.11.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" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
@@ -0,0 +1,72 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Net.WebSockets;
using FluentAssertions;
using Microsoft.Extensions.Logging;
namespace SystemExtensions.NetStandard.DependencyInjection.Tests.WebSockets;
[TestClass]
public sealed class ClientWebSocketResolverTests
{
private readonly ClientWebSocketResolver webSocketResolver = new();
private readonly Mock<Slim.IServiceProvider> serviceProviderMock = new();
[TestMethod]
public void CanResolve_IHttpClient_ReturnsTrue()
{
var type = typeof(IClientWebSocket<>);
var canResolve = this.webSocketResolver.CanResolve(type);
canResolve.Should().BeTrue();
}
[TestMethod]
public void CanResolve_AnythingElse_ReturnsFalse()
{
var types = new Type[] { typeof(ClientWebSocket), typeof(object), typeof(string), typeof(ClientWebSocketResolverTests), typeof(int) };
foreach (var type in types)
{
var canResolve = this.webSocketResolver.CanResolve(type);
canResolve.Should().BeFalse();
}
}
[TestMethod]
public void Resolve_TypedClient_ReturnsIClientWebSocketResolver()
{
var loggerMock = new Mock<ILogger<string>>();
this.serviceProviderMock.Setup(u => u.GetService(typeof(ILogger<string>)))
.Returns(loggerMock.Object);
var client = this.webSocketResolver.Resolve(this.serviceProviderMock.Object, typeof(IClientWebSocket<string>));
client.Should().BeAssignableTo<IClientWebSocket<string>>();
}
[TestMethod]
public void Resolve_NonGenericType_Throws()
{
Action action = new(() =>
{
this.webSocketResolver.Resolve(this.serviceProviderMock.Object, typeof(IClientWebSocket<>));
});
action.Should().Throw<Exception>();
}
[TestMethod]
public void Resolve_RandomType_Throws()
{
Action action = new(() =>
{
this.webSocketResolver.Resolve(this.serviceProviderMock.Object, typeof(string));
});
action.Should().Throw<Exception>();
}
}