Skip to content

Associated with IHubContext for Hub

Mateusz Soboń edited this page Feb 9, 2020 · 8 revisions

List of available mocks and helpfull verify methods

   public Mock<IGroupManager> GroupsMock { get; }
   public Mock<IHubContext<THub>> IHubContextMock { get; protected set; }
   public Mock<IHubClients> ClientsMock { get; protected set; }
   public Mock<IClientProxy> ClientsAllMock { get; protected set; }
   public Mock<IClientProxy> ClientsAllExceptMock { get; protected set; }
   public Mock<IClientProxy> ClientsClientMock { get; protected set; }
   public Mock<IClientProxy> ClientsClientsMock { get; protected set; }
   public Mock<IClientProxy> ClientsGroupMock { get; protected set; }
   public Mock<IClientProxy> ClientsGroupExceptMock { get; protected set; }
   public Mock<IClientProxy> ClientsGroupsMock { get; protected set; }
   public Mock<IClientProxy> ClientsUserMock { get; protected set; }
   public Mock<IClientProxy> ClientsUsersMock { get; protected set; }

   public void VerifySomebodyAddedToGroup(Times times);
   public void VerifySomebodyAddedToGroup(Times times, string groupName);
   public void VerifySomebodyAddedToGroup(Times times, string groupName, string connectionId);
   public void VerifySomebodyRemovedFromGroup(Times times);
   public void VerifySomebodyRemovedFromGroup(Times times, string groupName);
   public void VerifySomebodyRemovedFromGroup(Times times, string groupName, string connectionId);
   public void VerifyUserAddedToGroupByConnId(Times times, string connectionId);
   public void VerifyUserRemovedFromGroupByConnId(Times times, string connectionId);

How use it?

  1. In your test create instance of UnitTestingSupportForIHubContext<TNonGenericHub> where TNonGenericHub : Hub.
  2. Inject UnitTestingSupportForIHubContext.IHubContextMock.Object where you want (for example, service).
  3. Call tested method.
  4. Verify (examples below).

Example

 public void TestedMethodName_TestScenario_ExpectedResult()
 {
   //Arrange
   var iHubContextSupport 
            = new UnitTestingSupportForIHubContext<ExampleNonGenericHub>();
   var exampleService 
            = new ServiceWhichUseIHubContext(unitTestingSupport.IHubContextMock.Object);
   
   //Act
   //Assume NotifyAllAboutSomething call inside:
   //_exampleNonGenericHub
   //  .Clients
   //    .All
   //     .SendAsync("NotifyAboutSomethingAwesome", "First argument", "SecondArgument");
   exampleService.NotifyAllAboutSomething();

   //Assert
   //Here we must use SendCoreAsync instead of SendAsync, 
   //because SendAsync is static method (extension method)
   //This is no big problem see SendAsync vs SendCoreAsync link below.
   iHubContextSupport.ClientsAllMock
       .Verify(
           x => x.SendCoreAsync(
              "NotifyAboutSomethingAwesome", 
              new object[] {"First argument" ,  "SecondArgument"}, 
              It.IsAny<CancellationToken>())
       );
 }

Example #2

public void TestedMethodName_TestScenario_ExpectedResult()
 {
   //Arrange
   var iHubContextSupport 
           = new UnitTestingSupportForIHubContext<ExampleNonGenericHub>();
   var exampleService 
           = new ServiceWhichUseIHubContext(unitTestingSupport.IHubContextMock.Object);
   
   //Act
   //Assume AddUserToChatRoom call inside:
   //_exampleNonGenericHub
   //    .Groups
   //       .AddToGroupAsync("connId", "groupName");
   exampleService.AddUserToChatRoom("groupName");

   //Assert
   iHubContextSupport
         .VerifySomebodyAddedToGroup(Times.Once(), "groupName");
 }
Clone this wiki locally