-
Hi, I am a new user who has managed to test rendering of a Blazor server page that uses a seconday signalR client connection. I have implemented a factory ( I am have written a test to check that the Am I using bUnit correctly in the example below? Test [Fact]
public async Task Index_Page_DisposeAsync_Closes()
{
var _FactoryHubMock = new Mock<IHubProxyFactory>();
var _HubProxyMock = new Mock<IHubProxy>();
_FactoryHubMock.Setup(x => x.Create(It.IsAny<string>(), It.IsAny<JsonSerializerOptions>()))
.Returns(_HubProxyMock.Object).Verifiable();
Services.AddScoped<ILogger<MotionDetectionRepository>, NullLogger<MotionDetectionRepository>>();
Services.AddScoped<ILogger<MotionInfoConverter>, NullLogger<MotionInfoConverter>>();
Services.AddScoped<ILogger<MotionDetectionConverter>, NullLogger<MotionDetectionConverter>>();
Services.AddScoped<ILogger<JsonVisitor>, NullLogger<JsonVisitor>>();
Services.AddScoped<ILogger<WebApp.Pages.Index>, NullLogger<WebApp.Pages.Index>>();
Services.AddScoped<IMotionDetectionRepository>(sp => _Repository);
Services.AddScoped<MockNavigationManager>();
Services.AddScoped<IHubProxyFactory, HubProxyFactory>();
_HubProxyMock.Setup(x => x.DisposeAsync()).Returns(Task.CompletedTask).Verifiable();
var indexPage = RenderComponent<WebApp.Pages.Index>().Instance;
await indexPage.DisposeAsync();
_HubProxyMock.Verify();
} Blaor Server Page SUT public partial class Index : IAsyncDisposable, IDisposable
{
private IHubProxy hubConnection;
private bool disposed = false;
[Inject]
public IHubProxyFactory HubConnectionBuilder { get; set; }
/// other injected services omitted for brevity
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
var hubUrl = NavigationManager.BaseUri.TrimEnd('/') + "/motionhub";
try
{
Logger.LogInformation("Index.razor page is performing initial render, connecting to secondary signalR hub");
hubConnection = HubConnectionBuilder.Create(
hubUrl,
JsonConvertersFactory.CreateDefaultJsonConverters
(
LoggerMotionDetection,
LoggerMotionInfo,
LoggerJsonVisitor
)
);
hubConnection.On<MotionDetection>("ReceiveMotionDetection", ReceiveMessage);
hubConnection.Closed += CloseHandler;
Logger.LogInformation("Starting HubConnection");
await hubConnection.StartAsync();
Logger.LogInformation("Index Razor Page initialised, listening on signalR hub => " + hubUrl.ToString());
}
catch (Exception e)
{
Logger.LogError(e, "Encountered exception => " + e);
}
}
}
public async virtual ValueTask DisposeAsync()
{
try
{
if (hubConnection != null)
{
Logger.LogInformation("Closing secondary signalR connection...");
try { await hubConnection.StopAsync(); }
finally
{
await hubConnection.DisposeAsync();
}
Logger.LogInformation("Closed secondary signalR connection");
}
// Dispose(); When migrated to ASP.NET Core 5 let DisposeAsync trigger Dispose
}
catch (Exception exception)
{
Logger.LogInformation($"Exception encountered wwhile stopping secondary signalR connection :: {exception.Message}");
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Solved it....i was not adding the factory mock object to the services correctly! [Fact]
public async Task Index_Page_DisposeAsync_Closes()
{
var _FactoryHubMock = new Mock<IHubProxyFactory>();
var _HubProxyMock = new Mock<IHubProxy>();
_HubProxyMock.Setup(x => x.DisposeAsync()).Returns(Task.CompletedTask).Verifiable();
_FactoryHubMock.Setup(x => x.Create(It.IsAny<string>(), It.IsAny<JsonSerializerOptions>()))
.Returns(_HubProxyMock.Object).Verifiable();
Services.AddScoped<ILogger<MotionDetectionRepository>, NullLogger<MotionDetectionRepository>>();
Services.AddScoped<ILogger<MotionInfoConverter>, NullLogger<MotionInfoConverter>>();
Services.AddScoped<ILogger<MotionDetectionConverter>, NullLogger<MotionDetectionConverter>>();
Services.AddScoped<ILogger<JsonVisitor>, NullLogger<JsonVisitor>>();
Services.AddScoped<ILogger<WebApp.Pages.Index>, NullLogger<WebApp.Pages.Index>>();
Services.AddScoped<IMotionDetectionRepository>(sp => _Repository);
Services.AddScoped<MockNavigationManager>();
Services.AddSingleton(typeof(IHubProxyFactory), _FactoryHubMock.Object);
var indexPage = RenderComponent<WebApp.Pages.Index>().Instance;
await indexPage.DisposeAsync();
_HubProxyMock.Verify();
_FactoryHubMock.Verify();
} |
Beta Was this translation helpful? Give feedback.
Solved it....i was not adding the factory mock object to the services correctly!