Skip to content

Associated with IHubContext for HubT

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

List of available mocks and helpfull verify methods

   public Mock<IGroupManager> GroupsMock { get; }
   public Mock<IHubContext<THub, THubResponses>> IHubContextMock { get; protected set; }
   public Mock<IHubClients<THubResponses>> ClientsMock { get; protected set; }
   public Mock<THubResponses> ClientsAllMock { get; protected set; }
   public Mock<THubResponses> ClientsAllExceptMock { get; protected set; }
   public Mock<THubResponses> ClientsClientMock { get; protected set; }
   public Mock<THubResponses> ClientsClientsMock { get; protected set; }
   public Mock<THubResponses> ClientsGroupMock { get; protected set; }
   public Mock<THubResponses> ClientsGroupExceptMock { get; protected set; }
   public Mock<THubResponses> ClientsGroupsMock { get; protected set; }
   public Mock<THubResponses> ClientsUserMock { get; protected set; }
   public Mock<THubResponses> 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<THub, TIHubResponses>.
  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<ExampleGenericHub, IGenericHubResponses>();
   var exampleService 
           = new ServiceWhichUseIHubContext(iHubContextSupport.IHubContextMock.Object);
   
   //Act
   //Assume NotifyAllAboutSomething call inside:
   //Clients
   //  .All
   //     .NotifyAboutSomethingAwesome("First argument", "SecondArgument");
   exampleService.NotifyAllAboutSomething();

   //Assert
   iHubContextSupport.ClientsAllMock
       .Verify(
           x => x.NotifyAboutSomethingAwesome("First argument" ,  "SecondArgument"),
           Times.Once()
       );
 }

Example #2

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

   //Assert
   iHubContextSupport
         .VerifySomebodyAddedToGroup(Times.Once(), "groupName");
 }

Clone this wiki locally