Fix тесты

This commit is contained in:
Roman Efremov 2024-11-13 15:38:13 +05:00
parent 9196dc1f9c
commit 7ffdea28ab
8 changed files with 117 additions and 123 deletions

View File

@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
namespace ExampleSignalR.Test
{
public class AppFixture
{
public const string BaseUrl = "http://localhost:54321";
static AppFixture()
{
var webhost = WebHost
.CreateDefaultBuilder(null)
.UseStartup<IStartup>()
.UseUrls(BaseUrl)
.Build();
webhost.Start();
}
}
}

View File

@ -0,0 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ExampleSignalR\ExampleSignalR.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
@ -9,18 +9,6 @@
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />

View File

@ -0,0 +1,9 @@
namespace ExampleSignalR.Test.Services;
public class MessageServiceTest() : IMessageService
{
public string GetMessage()
{
return "321";
}
}

View File

@ -0,0 +1,63 @@
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
namespace ExampleSignalR.Test.Tests;
public class HubTest : BaseIntegrationTest
{
private const string url = $"wss://localhost/chatHub";
private readonly WebAppFactoryFixture _factory;
public HubTest(WebAppFactoryFixture factory) : base(factory)
{
_factory = factory;
}
[Fact]
public async Task ConnectToSignalRHub()
{
var hubConnection = await CreateHubConnection();
}
[Fact]
public async Task SignalRHubResponsibility()
{
var hubConnection = await CreateHubConnection();
bool success = false;
hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
{
using var scope = _factory.Services.CreateScope();
var messageService = scope.ServiceProvider.GetRequiredService<IMessageService>();
var testMessage = messageService.GetMessage();
if (message == testMessage)
{
success = true;
}
});
await hubConnection.InvokeAsync("SendMessage", nameof(HubTest), "test");
await Task.Delay(10000);
if (!success)
{
throw new Exception();
}
}
private async Task<HubConnection> CreateHubConnection()
{
var server = _factory.Server;
var hubConnection = new HubConnectionBuilder()
.WithUrl(url, options =>
{
options.HttpMessageHandlerFactory = _ => server.CreateHandler();
})
.Build();
await hubConnection.StartAsync();
return hubConnection;
}
}

View File

@ -1,38 +0,0 @@
using Microsoft.AspNetCore.SignalR.Client;
using static System.Net.WebRequestMethods;
using static Microsoft.AspNetCore.Http.StatusCodes;
namespace ExampleSignalR.Test;
public class UnitTest1 : BaseIntegrationTest
{
private readonly HttpClient _httpClient;
public UnitTest1(WebAppFactoryFixture factory) : base(factory)
{
_httpClient = factory.CreateClient();
}
[Fact]
public async void ConnectToSignalRHub()
{
var hubConnection = new HubConnectionBuilder()
.WithUrl($"{factory.url}/chatHub") // ...appsettings.json
.Build();
await hubConnection.StartAsync();
}
//public async Task Connect()
//{
// try
// {
// await hubConnection.StartAsync();
// Console.WriteLine("×àò ïîäêëþ÷åí...");
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Îøèáêà ïîäêëþ÷åíèÿ: {ex.Message}");
// }
//}
}

View File

@ -1,50 +1,22 @@
using System.Net.Http.Headers;
using System.Text.Json;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using ExampleSignalR.Test.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using static Microsoft.AspNetCore.Http.StatusCodes;
namespace ExampleSignalR.Test;
public class WebAppFactoryFixture : WebApplicationFactory<ExampleSignalR.Program>
{
public string url = "https://localhost:7083";
//private static readonly JsonSerializerOptions JsonSerializerOptions = new()
//{
//};
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(ConfigureServices);
//builder.UseEnvironment("Development");
builder.UseUrls(url);
base.ConfigureWebHost(builder);
}
private void ConfigureServices(IServiceCollection services)
{
//var t = services.Select(e => e.ServiceType == typeof(IMessageService));
services.RemoveAll(typeof(IMessageService));
services.AddTransient<IMessageService, MessageServiceT>();
services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = Status307TemporaryRedirect;
options.HttpsPort = 7083;
});
}
}
public class MessageServiceT() : IMessageService
{
public string GetMessage()
{
return "321";
services.AddTransient<IMessageService, MessageServiceTest>();
}
}

View File

@ -1,16 +0,0 @@
{
"Kestrel": {
"Endpoints": {
"Https": {
"Url": "https://localhost:7083"
}
}
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}