Page 1 of 1

Was ist die korrekte Implementierung für das Testen einer WPF -Anwendung mit Abhängigkeitsinjektion für Unit -Testen?

Posted: 03 Apr 2025, 06:53
by Anonymous
Ich habe eine gemeinsame Anwendungsweite der Singleton-Modellklasse (Projekt) durch Abhängigkeitsinjektion.
funktioniert in der Anwendung hervorragend. Aber wegen der DI habe ich Schwierigkeiten, dies zu testen.public partial class App : Application
{
public IHost AppHost;

///
/// Default constructor
///
public App()
{
AppHost = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddSingleton();
})
.Build();
...
}
}
< /code>
public partial class Analysis
{
public void AnalysisMethod()
{
IProject project = ((App)Application.Current).AppHost.Services.GetRequiredService();
}
}
< /code>
I'm trying to unit test AnalysisMethod but of course it fails because there is no Application.Current. How should I be coding/testing this, keeping in mind that I'd like to keep a singleton IProject created by DI?