Testing SignalR Applications
Overview
This tutorial walks you through the process of testing real‑time SignalR applications using Microsoft.AspNetCore.SignalR.Client and xUnit. You’ll learn how to simulate hubs, write integration tests, and verify client‑side behavior.
Prerequisites
- .NET 6 SDK or later
- Visual Studio 2022 or VS Code
- Basic knowledge of SignalR hubs
- Unit testing framework (xUnit)
Setting Up the Test Project
Create a separate test project to keep your production code clean.
dotnet new xunit -n SignalR.Tests
cd SignalR.Tests
dotnet add package Microsoft.AspNetCore.SignalR.Client
dotnet add package Microsoft.AspNetCore.SignalR.Protocols.MessagePack
dotnet add reference ../YourSignalRApp/YourSignalRApp.csproj
Mocking a Hub for Tests
Use the in‑memory TestServer to host a hub during tests.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.SignalR;
public class TestHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
public static class TestServerFactory
{
public static TestServer Create()
{
var builder = new WebHostBuilder()
.ConfigureServices(s => s.AddSignalR().AddMessagePackProtocol())
.Configure(app => app.UseRouting()
.UseEndpoints(e => e.MapHub<TestHub>("/testhub")));
return new TestServer(builder);
}
}
Writing a Client Integration Test
Connect to the in‑memory hub and assert messages.
using Microsoft.AspNetCore.TestHost;
using Microsoft.AspNetCore.SignalR.Client;
using Xunit;
public class SignalRIntegrationTests
{
[Fact]
public async Task BroadcastMessage_IsReceivedByAllClients()
{
using var server = TestServerFactory.Create();
var hubConnection = new HubConnectionBuilder()
.WithUrl(server.BaseAddress + "testhub", options => options.HttpMessageHandlerFactory = _ => server.CreateHandler())
.AddMessagePackProtocol()
.Build();
string receivedUser = null;
string receivedMessage = null;
hubConnection.On<string, string>("ReceiveMessage", (user, msg) =>
{
receivedUser = user;
receivedMessage = msg;
});
await hubConnection.StartAsync();
await hubConnection.InvokeAsync("SendMessage", "tester", "Hello SignalR");
// Give the hub a moment to push the message
await Task.Delay(100);
Assert.Equal("tester", receivedUser);
Assert.Equal("Hello SignalR", receivedMessage);
await hubConnection.StopAsync();
}
}
Debugging Tips
- Enable detailed logs:
.ConfigureLogging(l => l.AddDebug()) - Use
TestServer.CreateHandler()to capture HTTP traffic. - When a test hangs, increase the
Task.Delayor useCancellationToken.
Summary
You now have a solid foundation for testing SignalR hubs using an in‑memory server and client. Integrate these patterns into your CI pipeline to catch regressions early.