-
Notifications
You must be signed in to change notification settings - Fork 1
Associated with IHubContext for Hub
Mateusz Soboń edited this page Feb 9, 2020
·
8 revisions
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);
- In your test create instance of
UnitTestingSupportForIHubContext<TNonGenericHub> where TNonGenericHub : Hub
. - Inject
UnitTestingSupportForIHubContext.IHubContextMock.Object
where you want (for example, service). - Call tested method.
- Verify (examples below).
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>())
);
}
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");
}