-
Notifications
You must be signed in to change notification settings - Fork 1
How use testing support if you don't want (or you can`t) use provided by me base classes or your testing framework don't support before and after each test code execution features
All support for hubs testing is in
SignalR_UnitTestingSupportCommon.Hubs
namespace.
All support for testing with IHubContext is in
SignalR_UnitTestingSupportCommon.IHubContextSupport
namespace.
- Download AspNetCore.SignalR.UnitTestingSupport.Common nuget: https://www.nuget.org/packages/AspNetCore.SignalR.UnitTestingSupport.Common/
At this point support for IHubContext is available. See: IHubContext for Hub or IHubContext for HubT. If you want testing hubs read how to use below.
Create instance HubUnitTestsSupport
.
Create instance HubUnitTestsSupport<T>
.
Create instance HubUnitTestsWithEFSupport<TDbContext>
.
Create instance HubUnitTestsWithEFSupport<T, TDbContext>
.
After initialization support class call SetUp
method on it's instance.
For testing with EF: Remember to call TearDown
on support class instance after test (and make sure it`s called even if exception is thrown during the test).
public void TestedMethodName_TestScenario_ExpectedResult()
{
var support = new HubUnitTestsSupport();
support.SetUp();
var exampleHub = new ExampleHub();
support.AssignToHubRequiredProperties(exampleHub);
//Assume DoSomething call inside:
//Clients
// .Caller
// .SendAsync("NotifyAboutSomethingAwesome", "First argument", "SecondArgument");
exampleHub.DoSomething();
support
.ClientsCallerMock
.Verify(
x => x.SendCoreAsync(
"NotifyAboutSomethingAwesome",
new object[] {"First argument" , "SecondArgument"},
It.IsAny<CancellationToken>())
);
}
public void TestedMethodName_TestScenario_ExpectedResult()
{
var exampleHub = new ExampleHub();
AssignToHubRequiredProperties(exampleHub);
//Assume DoSomething call inside:
//Clients
// .Caller
// .SendAsync("NotifyAboutSomethingAwesome", "First argument", "SecondArgument");
exampleHub.DoSomething();
ClientsCallerMock
.Verify(
x => x.SendCoreAsync(
"NotifyAboutSomethingAwesome",
new object[] {"First argument" , "SecondArgument"},
It.IsAny<CancellationToken>())
);
}
As you can see this approaches are really similar. All differences:
var support = new HubUnitTestsSupport();
support.SetUp();
...
support.AssignToHubRequiredProperties(exampleHub);
...
support
.ClientsCallerMock
Then you can use same docs as for base classes. Base classes are just a facades which automatically call setup and teardown methods using testing frameworks). If you want understand how it work better see this.