Files
ReLibreHardwareMonitor/LibreHardwareMonitor.Windows.WinUI.Tests/Composition/ServiceCollectionExtensionsTests.cs
T
JMR-devandCopilot dcb6a5b803 Add DI container and view-model tests
Add a composition-root test that builds the provider with ValidateOnBuild to catch missing registrations (without touching the real hardware monitor), plus per-service registration assertions. Add a MainWindowViewModel test that constructs the view model entirely from faked IHardwareMonitorService/ILogger/IRemoteWebServer, proving the testability win. To make that possible, inject the UI DispatcherQueue (captured on the UI thread by the composition factory) instead of calling the static DispatcherQueue.GetForCurrentThread() in the view-model constructor, which threw in the headless test host; the tree-rebuild marshalling now treats a null dispatcher as direct invocation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-02 15:20:40 -05:00

50 lines
2.1 KiB
C#

// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Copyright (C) LibreHardwareMonitor and Contributors.
using System.Linq;
using LibreHardwareMonitor.Windows.WinUI.Composition;
using LibreHardwareMonitor.Windows.WinUI.Services;
using LibreHardwareMonitor.Windows.WinUI.Services.Tracing;
using LibreHardwareMonitor.Windows.WinUI.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace LibreHardwareMonitor.Windows.WinUI.Tests.Composition;
// Guards the single composition root: every dependency referenced by a registered service must
// itself be registered, otherwise the app fails to resolve its graph at launch. ValidateOnBuild
// builds the dependency call sites without instantiating the services, so this stays off the real
// hardware-monitor / kernel-driver path while still catching missing registrations.
public class ServiceCollectionExtensionsTests
{
[Fact]
public void AddAppServices_BuildsProviderWithoutMissingRegistrations()
{
ServiceProvider provider = new ServiceCollection()
.AddAppServices(NoOpStartupTracer.Instance)
.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true });
Assert.NotNull(provider);
provider.Dispose();
}
[Theory]
[InlineData(typeof(AppSettings))]
[InlineData(typeof(IStartupTracer))]
[InlineData(typeof(IHardwareMonitorService))]
[InlineData(typeof(ILogger))]
[InlineData(typeof(IRemoteWebServer))]
[InlineData(typeof(SensorSelectionService))]
[InlineData(typeof(PlotTrackingService))]
[InlineData(typeof(StartupService))]
[InlineData(typeof(MainWindowViewModel))]
[InlineData(typeof(IMainWindowRuntimeFactory))]
public void AddAppServices_RegistersExpectedService(System.Type serviceType)
{
IServiceCollection services = new ServiceCollection().AddAppServices(NoOpStartupTracer.Instance);
Assert.Contains(services, descriptor => descriptor.ServiceType == serviceType);
}
}