Skip to content

How use testing support if you don't want (or you can`t) use provided by me base classes or your testing framework don't support before and after each test code execution features

NightAngell edited this page Feb 27, 2019 · 13 revisions

All support for hubs testing is in SignalR_UnitTestingSupportCommon.Hubs namespace.

All support for testing with IHubContext is in SignalR_UnitTestingSupportCommon.IHubContextSupport namespace.

Installation

  1. Download AspNetCore.SignalR.UnitTestingSupport.Common nuget: https://www.nuget.org/packages/AspNetCore.SignalR.UnitTestingSupport.Common/

At this point support for IHubContext is available. See: IHubContext for Hub or IHubContext for HubT. If you want testing hubs read how to use below.

How to use (example below)

1) Initialization

For testing Hub

Create instance HubUnitTestsSupport.

For testing Hub<T>

Create instance HubUnitTestsSupport<T>.

For testing Hub with Entity Framework Core

Create instance HubUnitTestsWithEFSupport<TDbContext>.

For testing Hub<T> with Entity Framework Core

Create instance HubUnitTestsWithEFSupport<T, TDbContext>.

2) Setup before test and clear after test

After initialization support class call SetUp method on it's instance.
For testing with EF: Remember to call TearDown on support class instance after test (and make sure it`s called even if exception is thrown during the test).

3) Call tested method

4) Verify (Example below)

Unit testing support base classes vs direct initialization example

Same test with initialization approach

  public void TestedMethodName_TestScenario_ExpectedResult()
  {
     var support = new HubUnitTestsSupport();
     support.SetUp();
     var exampleHub = new ExampleHub();
     support.AssignToHubRequiredProperties(exampleHub);
     
     //Assume DoSomething call inside:
     //Clients
     //  .Caller
     //     .SendAsync("NotifyAboutSomethingAwesome", "First argument", "SecondArgument");
     exampleHub.DoSomething();

     support
       .ClientsCallerMock
       .Verify(
          x => x.SendCoreAsync(
             "NotifyAboutSomethingAwesome", 
             new object[] {"First argument" ,  "SecondArgument"}, 
             It.IsAny<CancellationToken>())
       );
  }

Same test with base class inheritance approach

  public void TestedMethodName_TestScenario_ExpectedResult()
  {
     var exampleHub = new ExampleHub();
     AssignToHubRequiredProperties(exampleHub);
     
     //Assume DoSomething call inside:
     //Clients
     //  .Caller
     //     .SendAsync("NotifyAboutSomethingAwesome", "First argument", "SecondArgument");
     exampleHub.DoSomething();

     ClientsCallerMock
       .Verify(
          x => x.SendCoreAsync(
             "NotifyAboutSomethingAwesome", 
             new object[] {"First argument" ,  "SecondArgument"}, 
             It.IsAny<CancellationToken>())
       );
  }

As you can see this approaches are really similar. All differences:

   var support = new HubUnitTestsSupport();
   support.SetUp();
   ...
   support.AssignToHubRequiredProperties(exampleHub);
   ...
   support
      .ClientsCallerMock

Then you can use same docs as for base classes. Base classes are just a facades which automatically call setup and teardown methods using testing frameworks). If you want understand how it work better see this.

Clone this wiki locally