Skip to content

How to Unit Test SignalR #11052

Open
Open

Description

@BlueDragon709

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.

Activity

  1. changed the title [-]aspnetcore/signalr/ Tutorial for unit testing signalr[/-] [+]Doc request: aspnetcore/signalr/ Tutorial for unit testing signalr[/+] on Feb 22, 2019
  2. NightAngell commented on Feb 25, 2019

    @NightAngell

    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:

    1. Project site
    2. Docs

    There are examples in docs too.

  3. added this to the Backlog milestone on Feb 25, 2019
  4. Kiril1512 commented on Nov 7, 2019

    @Kiril1512

    Yes, this would be very helpful for developers not only for testing, but also do understand better SignalR through unit tests.

  5. chrispickford commented on Feb 16, 2021

    @chrispickford

    Adding my +1 as unit testing documentation would be extremely helpful.

  6. DennisvanBeelen commented on Mar 11, 2021

    @DennisvanBeelen

    Yes especially te proper way to mock Hubs and Clients would be super usefull.

  7. ivanrodriguezfernandez commented on Mar 18, 2021

    @ivanrodriguezfernandez

    +1
    Any news on this?

  8. segadora commented on Jun 29, 2021

    @segadora

    +1

  9. jacekkulis commented on Jul 16, 2021

    @jacekkulis

    +1

  10. 6 remaining items

  11. Diomedes1990 commented on Jul 28, 2022

    @Diomedes1990

    yes, this would be great, I was trying with old documentation.

  12. fn-bruce commented on Jul 29, 2022

    @fn-bruce

    +1

  13. sankarbha commented on Sep 29, 2022

    @sankarbha

    yes, pl. share any documentation or sample unit test projects. Thanks.

  14. postmeback commented on May 11, 2023

    @postmeback
    Contributor

    Any update ?

  15. eranbar commented on Jun 20, 2023

    @eranbar

    any updates?

  16. WeihanLi commented on Dec 27, 2023

    @WeihanLi
    Contributor

    Any update?

    Hope for the unit testing support not only for the server but also for the client

  17. DomenPigeon commented on Jan 21, 2024

    @DomenPigeon

    +1

  18. Kurren123 commented on Oct 14, 2024

    @Kurren123

    +1

  19. wadepickett commented on Sep 11, 2026

    @wadepickett
    Contributor

    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

  20. self-assigned this
    on Sep 11, 2026
  21. added
    ai-triage-action-plan
    on Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

SignalRai-triage-action-plandoc-bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions