Skip to content

Common for all test classes

NightAngell edited this page Feb 27, 2019 · 12 revisions

Assign required properties to hub (Important for all test clases)

AssignToHubRequiredProperties is responsible for assign Context, Groups, Clients mocks objects to Hub (Hub<T>).

Throws NullArgumentException if We pass null as parameter

  //For Hub
  public void AssignToHubRequiredProperties(Hub hub);

  //For Hub<T>
  public void AssignToHubRequiredProperties(Hub<T> hub);

For example:

 public void TestedMethodName_TestScenario_ExpectedResult()
 {
    var exampleHub = new ExampleHub();
    AssignToHubRequiredProperties(exampleHub);
    
    //Act
    //Assert
 }

ContextMock

//Mock for Microsoft.AspNetCore.SignalR.Hub.Context
public Mock<HubCallerContext> ContextMock { get; internal set; }

It has only two setups.

ContextMock.Setup(x => x.Items).Returns(ItemsFake);

//Conn id is guid. It is different in each test. But is same in scope of test.
ContextMock.Setup(x => x.ConnectionId).Returns(connId);

If you get NullReferenceException when you try testing using my lib, probably you forgot to setup another required for your test case property of Context.

ItemsFake

 //Fake for Microsoft.AspNetCore.SignalR.Hub.Context.Items
 public Dictionary<object, object> ItemsFake { get; internal set; }

Example of use:

 public void TestedMethodName_TestScenario_ExpectedResult()
 {
    //Arrange
    var exampleHub = new ExampleHub();
    AssignToHubRequiredProperties(exampleHub);
    
    //You can add something before test
    ItemsFake.Add("key", "value");
    
    //Act
    ...
    
    //Assert
    if(!ItemsFake.ContainsKey("key"))
    {
      throw new Exception("Context items not contain required key")
    }

    if(!ItemsFake.ContainsValue("value"))
    {
      throw new Exception("Context items not contain required value")
    }

    VerifyContextItemsContainKeyValuePair("key", "value");
 }

Usefull methods assoctiated with ItemsFake

 public void VerifyContextItemsContainKeyValuePair(object key, object value)

GroupsMock

//When you testing Hubs: Mock for Microsoft.AspNetCore.SignalR.Hub.Groups
//When you testing with IHubContext: Mock for Microsoft Microsoft.AspNetCore.SignalR.IHubContext.Groups
public Mock<IGroupManager> GroupsMock { get; internal set; }

Usefull methods assoctiated with GroupsMocs

This all methods throw Moq.MockException if verify test is invalid

 public void VerifySomebodyAddedToGroup(Times times);
 public void VerifySomebodyAddedToGroup(Times times, string groupName);
 public void VerifySomebodyAddedToGroup(Times times, string groupName, string connectionId);
 public void VerifyUserAddedToGroupByConnId(Times times, 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 VerifyUserRemovedFromGroupByConnId(Times times, string connectionId);

Of course instead of this methods you can use GroupsMocks directly, for example:

 //Instead use this
 public void VerifySomebodyRemovedFromGroup(Times times, string groupName, string connectionId)
 //Use this
 GroupsMock
   .Verify(x => x.RemoveFromGroupAsync(
       connectionId,
       groupName,
       It.IsAny<CancellationToken>()),
       times
 );

Example of use:

 public void TestedMethodName_TestScenario_ExpectedResult()
 {
    var exampleHub = new ExampleHub();
    AssignToHubRequiredProperties(exampleHub);
    
    exampleHub.AddSomebodyToGroup();

    VerifySomebodyAddedToGroup(Times.Once(), "AwesomeGroup");
 }
Clone this wiki locally