You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Doc request for unit testing signalr in a ASP.NetCore 2.2 app
I am currently working on a signalr application and I want to be able to test my hubs. I couldn't find any information about unit testing signalr in AspNetCore/SignalR. The only information that i could find was from 2013 for AspNet/SignalR and is pretty outdated.
I am sugesting to put the topic about unit testing signalr on this location:
Docs/ASP.NET/ASP.NET Core/ASP.NET Core 2.2/Real-time apps/Unit testing
or
Docs/ASP.NET/ASP.NET Core/ASP.NET Core 2.2/Test, debug and roubleshoot/Unit testing/signalr hubs
This topic will cover the following topics:
Testing of hubs, testing of groups and examples.
A rough outline:
Prerequisites
Testing hubs
-- Using MSTest
-- Using NUnit
-- Using xUnit
Testing groups
-- Using MSTest
-- Using NUnit
-- Using xUnit
Examples
I hope this gets added because there is not a lot of information on this topic.
Reacted by Tmr, Michael DePouw, Benjamin Crosnier, Guillaume Lacasa, Xavier, jfhr, ConMak, Max Merz, Roman Yakym, Kyrylo Yavorenko and 28 more
changed the title [-]aspnetcore/signalr/ Tutorial for unit testing signalr[/-][+]Doc request: aspnetcore/signalr/ Tutorial for unit testing signalr[/+]on Feb 22, 2019
I wrote lib (available via nuget) which provide support for unit testing in signalR core (both Hub (Hub<T>) and IHubContext<T> (IHubContext<T, P>) ). I wrote docs for it too then if you want you can easily use it. In short summary it provide preconfigured mocks which are auto set upped by testing framework (NUnit, xUnit, MsTest, but there is also info how to use it with custom testing framework in docs) and some helpful 'verify' methods. For testing hubs you need just add nuget to your project and inherit from provided by lib base class (there is also possibility to use it without base class, info in docs). I don't want duplicate docs, then if you are interested:
Doc request: Unit and integration testing SignalR hubs (ASP.NET Core, .NET 10)
There's still no dedicated guidance in the ASP.NET Core docs for testing SignalR. The only related content (diagnostics.md, troubleshoot.md) covers logging/diagnostics, not writing tests. This request replaces the original .NET 2.2 scope (2019) and retargets it to .NET 10 and current tooling.
Why this is needed
Testing SignalR has changed substantially since the original request:
Hubs are now typically wired up with minimal hosting (WebApplication / top-level statements in Program.cs), not Startup.
Integration testing patterns rely on WebApplicationFactory + TestServer, which SignalR clients can connect to via a custom HttpMessageHandler.
Tooling has moved on: xunit.v3, Microsoft Testing Platform (MTP), NSubstitute/Moq, and the newer strongly-typed hub testing surface.
Proposed location
aspnetcore/signalr/test.md (new topic), cross-linked from hubs.md and the test/debug/troubleshoot section.
Proposed outline
Prerequisites
.NET 10 SDK, a test project, and choice of framework (xUnit v3 recommended; NUnit/MSTest noted as alternatives)
Mocking library (NSubstitute or Moq)
Unit testing a hub in isolation (no server)
Setting Hub.Clients, Hub.Context, and Hub.Groups with mocked IHubCallerClients, HubCallerContext, and IGroupManager
Verifying calls to Clients.All, Clients.Caller, Clients.Group(...) via SendCoreAsync with expected method name and arguments
Testing strongly-typed hubs (Hub) and asserting on the typed client interface
Testing OnConnectedAsync / OnDisconnectedAsync and group add/remove logic
Integration testing hubs over a real connection
Spinning up the app with WebApplicationFactory and using its Server.CreateHandler() to build a HubConnection against the in-memory TestServer
Registering client handlers with connection.On<...> and awaiting real round-trips (invoke → server → client callback)
Testing groups and broadcasts end-to-end with multiple client connections
Testing authentication/authorization on hubs (passing tokens/claims to the test connection)
Testing tips
Deterministic waits with TaskCompletionSource instead of arbitrary delays
Testing streaming hub methods (IAsyncEnumerable / channels)
Using hub filters and IHubContext in tests
Running tests in Visual Studio / CLI
Test Explorer discovery, running/debugging individual hub tests
dotnet test and Microsoft Testing Platform notes for xUnit v3
Complete samples
A unit-test sample project and an integration-test sample project for .NET 10
Activity
[-]aspnetcore/signalr/ Tutorial for unit testing signalr[/-][+]Doc request: aspnetcore/signalr/ Tutorial for unit testing signalr[/+]NightAngell commentedon Feb 25, 2019
I wrote lib (available via nuget) which provide support for unit testing in signalR core (both
Hub (Hub<T>) and IHubContext<T> (IHubContext<T, P>)). I wrote docs for it too then if you want you can easily use it. In short summary it provide preconfigured mocks which are auto set upped by testing framework (NUnit, xUnit, MsTest, but there is also info how to use it with custom testing framework in docs) and some helpful'verify' methods. For testing hubs you need just add nuget to your project and inherit from provided by lib base class (there is also possibility to use it without base class, info in docs). I don't want duplicate docs, then if you are interested:There are examples in docs too.
Kiril1512 commentedon Nov 7, 2019
Yes, this would be very helpful for developers not only for testing, but also do understand better SignalR through unit tests.
chrispickford commentedon Feb 16, 2021
Adding my +1 as unit testing documentation would be extremely helpful.
DennisvanBeelen commentedon Mar 11, 2021
Yes especially te proper way to mock Hubs and Clients would be super usefull.
ivanrodriguezfernandez commentedon Mar 18, 2021
+1
Any news on this?
segadora commentedon Jun 29, 2021
+1
jacekkulis commentedon Jul 16, 2021
+1
6 remaining items
Diomedes1990 commentedon Jul 28, 2022
yes, this would be great, I was trying with old documentation.
fn-bruce commentedon Jul 29, 2022
+1
sankarbha commentedon Sep 29, 2022
yes, pl. share any documentation or sample unit test projects. Thanks.
postmeback commentedon May 11, 2023
Any update ?
eranbar commentedon Jun 20, 2023
any updates?
WeihanLi commentedon Dec 27, 2023
Any update?
Hope for the unit testing support not only for the server but also for the client
DomenPigeon commentedon Jan 21, 2024
+1
Kurren123 commentedon Oct 14, 2024
+1
wadepickett commentedon Sep 11, 2026
Doc request: Unit and integration testing SignalR hubs (ASP.NET Core, .NET 10)
There's still no dedicated guidance in the ASP.NET Core docs for testing SignalR. The only related content (diagnostics.md, troubleshoot.md) covers logging/diagnostics, not writing tests. This request replaces the original .NET 2.2 scope (2019) and retargets it to .NET 10 and current tooling.
Why this is needed
Testing SignalR has changed substantially since the original request:
Hubs are now typically wired up with minimal hosting (WebApplication / top-level statements in Program.cs), not Startup.
Integration testing patterns rely on WebApplicationFactory + TestServer, which SignalR clients can connect to via a custom HttpMessageHandler.
Tooling has moved on: xunit.v3, Microsoft Testing Platform (MTP), NSubstitute/Moq, and the newer strongly-typed hub testing surface.
Proposed location
aspnetcore/signalr/test.md (new topic), cross-linked from hubs.md and the test/debug/troubleshoot section.
Proposed outline
Prerequisites
.NET 10 SDK, a test project, and choice of framework (xUnit v3 recommended; NUnit/MSTest noted as alternatives)
Mocking library (NSubstitute or Moq)
Unit testing a hub in isolation (no server)
Setting Hub.Clients, Hub.Context, and Hub.Groups with mocked IHubCallerClients, HubCallerContext, and IGroupManager
Verifying calls to Clients.All, Clients.Caller, Clients.Group(...) via SendCoreAsync with expected method name and arguments
Testing strongly-typed hubs (Hub) and asserting on the typed client interface
Testing OnConnectedAsync / OnDisconnectedAsync and group add/remove logic
Integration testing hubs over a real connection
Spinning up the app with WebApplicationFactory and using its Server.CreateHandler() to build a HubConnection against the in-memory TestServer
Registering client handlers with connection.On<...> and awaiting real round-trips (invoke → server → client callback)
Testing groups and broadcasts end-to-end with multiple client connections
Testing authentication/authorization on hubs (passing tokens/claims to the test connection)
Testing tips
Deterministic waits with TaskCompletionSource instead of arbitrary delays
Testing streaming hub methods (IAsyncEnumerable / channels)
Using hub filters and IHubContext in tests
Running tests in Visual Studio / CLI
Test Explorer discovery, running/debugging individual hub tests
dotnet test and Microsoft Testing Platform notes for xUnit v3
Complete samples
A unit-test sample project and an integration-test sample project for .NET 10