-
Notifications
You must be signed in to change notification settings - Fork 1
Common for all test classes
NightAngell edited this page Feb 23, 2019
·
12 revisions
AssignToHubRequiredPropertiesis responsible for assignContext, Groups, Clientsmocks objects toHub (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:
var exampleHub = new ExampleHub();
AssignToHubRequiredProperties(exampleHub);//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); //Fake for Microsoft.AspNetCore.SignalR.Hub.Context.Items
public Dictionary<object, object> ItemsFake { get; internal set; }Example of use:
ItemsFake.Add("key", "value");
...
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"); public void VerifyContextItemsContainKeyValuePair(object key, object value)//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; }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 TestingMethodName_TestScenario_ExpectedResult()
{
var exampleHub = new ExampleHub();
AssignToHubRequiredProperties(exampleHub);
exampleHub.AddSomebodyToGroup();
VerifySomebodyAddedToGroup(Times.Once(), "AwesomeGroup");
}