Skip to content

Associated with testing Hub

NightAngell edited this page Feb 23, 2019 · 19 revisions

Knowledge from basics and common are required to understand this topic.

List of preconfigured mocks.

For Context and Groups mocks see here.

   public Mock<IHubCallerClients> ClientsMock { get; private set; }

   public Mock<IClientProxy> ClientsAllMock { get; private set; }
   public Mock<IClientProxy> ClientsAllExceptMock { get; private set; }
   public Mock<IClientProxy> ClientsCallerMock { get; private set; }
   public Mock<IClientProxy> ClientsClientMock { get; private set; }
   public Mock<IClientProxy> ClientsClientsMock { get; private set; }
   public Mock<IClientProxy> ClientsGroupMock { get; private set; }
   public Mock<IClientProxy> ClientsGroupExceptMock { get; private set; }
   public Mock<IClientProxy> ClientsGroupsMock { get; private set; }
   public Mock<IClientProxy> ClientsOthersMock { get; private set; }
   public Mock<IClientProxy> ClientsOthersInGroupMock { get; private set; }
   public Mock<IClientProxy> ClientsUserMock { get; private set; }
   public Mock<IClientProxy> ClientsUsersMock { get; private set; }

How test Hub using this mocks?

Do not use ClientsMock directly to verify its child (child is for example: Clients.Caller). Use child mock instead to do this (for Clients.Caller it is ClientsCallerMock). Direct use of ClientsMock can cause unexpected behaviour when you try verify something.

  1. Create Hub
  2. Assign to Hub required properties using AssignToHubRequiredProperties
  3. Invoke Hub method
  4. Verify (Below example how do this)

For example

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

   //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 below.
   ClientsCallerMock
       .Verify(
           x => x.SendCoreAsync(
              "NotifyAboutSomethingAwesome", 
              new object[] {"First argument" ,  "SecondArgument"}, 
              It.IsAny<CancellationToken>())
       );
 }

SendAsync vs SendCoreAsync

SendAsync method is extension method (static method) then we cannot mock it. But, it use internally SendCoreAsync, then we can use it instead. For example:

 //Instead this
 ClientsCallerMock
       .Verify(
           x => x.SendAsync(
              "NotifyAboutSomethingAwesome", 
              "First argument",
              "SecondArgument", 
              It.IsAny<CancellationToken>())
       );

 //We can use this, and this simple trick resolve entire problem.
 ClientsCallerMock
       .Verify(
           x => x.SendCoreAsync(
              "NotifyAboutSomethingAwesome", 
              new object[] {"First argument" , "SecondArgument"}, 
              It.IsAny<CancellationToken>())
       );
Clone this wiki locally