feat(tests): Refaktorierung und Erweiterung von AuthClientTests für Verbindungs- und Nachrichtenbehandlung
- Ersetzte das statische Setup für die Client-Erstellung durch eine wiederverwendbare Build-Methode. - Ein neuer Test `StartAsync_ShouldConnectSuccessfully` wurde hinzugefügt, um den Verbindungsaufbau zu überprüfen. - Refactored `ReceiveMessage_ShouldCallOnMessageReceived` für die Einrichtung mehrerer Clients (Sender und Empfänger). - Konsolidierte hartkodierte URL in eine wiederverwendbare Konstante `HubUrl`.
This commit is contained in:
parent
cfe5df4b1d
commit
9fee7ea381
@ -7,18 +7,35 @@ namespace DigitalData.Auth.Tests.Client;
|
||||
[TestFixture]
|
||||
public class AuthClientTests
|
||||
{
|
||||
private IAuthClient _client;
|
||||
private static readonly string HubUrl = "https://localhost:7192/auth-hub";
|
||||
|
||||
private readonly static Func<Action<ClientParams>, ServiceProvider> Build = options => new ServiceCollection()
|
||||
.AddAuthHubClient(options)
|
||||
.BuildServiceProvider();
|
||||
private Func<Action<ClientParams>, ServiceProvider> Build;
|
||||
|
||||
[SetUp]
|
||||
public async Task Setup()
|
||||
public void Setup()
|
||||
{
|
||||
using var provider = Build(opt => opt.Url = "https://localhost:7192");
|
||||
_client = provider.GetRequiredService<IAuthClient>();
|
||||
await _client.StartAsync();
|
||||
Build = options => new ServiceCollection()
|
||||
.AddAuthHubClient(options)
|
||||
.BuildServiceProvider();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task StartAsync_ShouldConnectSuccessfully()
|
||||
{
|
||||
// Arrange
|
||||
using var provider = Build(opt => opt.Url = HubUrl);
|
||||
var client = provider.GetRequiredService<IAuthClient>();
|
||||
|
||||
// Act
|
||||
await client.TryStartAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(client.IsConnected, "Connected");
|
||||
Assert.That(!client.IsConnectionFailed, "There is no fail");
|
||||
Assert.That(client.ConnectionError, Is.Null);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -26,18 +43,25 @@ public class AuthClientTests
|
||||
{
|
||||
// Arrange
|
||||
bool wasCalled = false;
|
||||
using var provider = Build(opt =>
|
||||
|
||||
// Sender client
|
||||
using var provider_sender = Build(opt => opt.Url = HubUrl);
|
||||
var sender_client = provider_sender.GetRequiredService<IAuthClient>();
|
||||
await sender_client.TryStartAsync();
|
||||
|
||||
// Receiver client
|
||||
using var provider_receiver = Build(opt =>
|
||||
{
|
||||
opt.Url = "https://localhost:7192";
|
||||
opt.Url = HubUrl;
|
||||
opt.Events.OnMessageReceived = (user, message, logger) => wasCalled = true;
|
||||
});
|
||||
var client = provider.GetRequiredService<IAuthClient>();
|
||||
var client_receiver = provider_receiver.GetRequiredService<IAuthClient>();
|
||||
|
||||
string testUser = "TestUser";
|
||||
string testMessage = "Hello, World!";
|
||||
|
||||
// Act
|
||||
await client.ReceiveMessageAsync(testUser, testMessage);
|
||||
await client_receiver.ReceiveMessageAsync(testUser, testMessage);
|
||||
|
||||
// Assert
|
||||
Assert.That(wasCalled, Is.True, "OnMessageReceived was not called.");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user