Skip to content

Testability: All ViewModels coupled to Application.Current — cannot instantiate in tests #429

@Christophe-Rogiers

Description

@Christophe-Rogiers

Description

All Manager ViewModels cast Application.Current to App in their constructors or tick handlers to read configuration:

var app = (App)Application.Current;
_maxLines = app.ConsoleMaxLines;
_refreshInterval = app.RefreshIntervalInSeconds;

Affected files:

  • MainViewModel.cs (lines 364, 450, 528, 546, 854)
  • ConsoleViewModel.cs (lines 222, 366, 518)
  • PerformanceViewModel.cs (lines 181, 431)
  • DependenciesViewModel.cs (lines 205, 355)

This makes it impossible to instantiate these ViewModels in a unit test without a running WPF Application.

Suggested fix

Extract configuration into an injectable IAppConfiguration interface:

public interface IAppConfiguration
{
    int ConsoleMaxLines { get; }
    int RefreshIntervalInSeconds { get; }
    // ...
}

Inject it via constructor instead of accessing Application.Current.

Metadata

Metadata

Assignees

Labels

testsChanges to test code and coverage

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions