Skip to content

Associated with testing Hub T

NightAngell edited this page Feb 18, 2019 · 8 revisions

List of preconfigured mocks.

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

  public Mock<TIHubResponses> ClientsAllMock { get; private set; }
  public Mock<TIHubResponses> ClientsAllExceptMock { get; private set; }
  public Mock<TIHubResponses> ClientsCallerMock { get; private set; }
  public Mock<TIHubResponses> ClientsClientMock { get; private set; }
  public Mock<TIHubResponses> ClientsClientsMock { get; private set; }
  public Mock<TIHubResponses> ClientsGroupMock { get; private set; }
  public Mock<TIHubResponses> ClientsGroupExceptMock { get; private set; }
  public Mock<TIHubResponses> ClientsGroupsMock { get; private set; }
  public Mock<TIHubResponses> ClientsOthersMock { get; private set; }
  public Mock<TIHubResponses> ClientsOthersInGroupMock { get; private set; }
  public Mock<TIHubResponses> ClientsUserMock { get; private set; }
  public Mock<TIHubResponses> 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
   //    .NotifyAboutSomethingAwesome("First argument", "SecondArgument");
   exampleHub.DoSomething()

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

Clone this wiki locally