public static class RunicWindowTestHost Creates a RunicWindowTestHost from the application's services.
Methods
Create
public static RunicWindowTestHost<TViewModel> Create<TViewModel>(IServiceProvider services, RunicWindowTestHostOptions? options = null) where TViewModel : class Resolves the root ViewModel, its generated Bridge attachment, the View locator, the model context and the logger factory from services, as the Desktop and CS-WebUI windows do, and creates a test host for them. By default the window uses a fake clock and sequential ids. Before it resolves the ViewModel it passes the logger factory to the registered MVVM adapters, as those windows do, so the ReactiveUI model-context scheduler's warnings reach the test's logs.
Parameters
servicesThe window's service scope, such as
scope.ServiceProviderof a scope the test creates. The caller keeps ownership of the scope and the ViewModels it resolves.optionsThe root route, clock and id source. A View locator, model context or logger factory set here is used instead of the registered one.
Exceptions
InvalidOperationExceptionTViewModelor its generated Bridge is not registered.
Remarks
services needs what the application's window scope has: the ViewModel itself (for example services.AddScoped<ShellViewModel>()) and its generated Bridge, which the application's generated AddRunicViews() registers. Build them with the application's own registration method, so the test runs the same composition. The View locator, model context (for example from AddRunicReactiveModelContext() or AddRunicNavigation()) and logger factory are used when registered.
For a ViewModel the test builds by hand, pass it to RunicWindowTestHost.Create instead of registering it.
Create
public static RunicWindowTestHost<TViewModel> Create<TViewModel>(IServiceProvider services, TViewModel viewModel, RunicWindowTestHostOptions? options = null) where TViewModel : class Creates a test host for a ViewModel the test built, with its generated Bridge attachment, the View locator, the model context and the logger factory from services, as RunicWindowTestHost.Create does. The ViewModel does not need to be registered.
Parameters
servicesThe services with the application's generated
AddRunicViews()registrations.viewModelThe root ViewModel; the caller keeps ownership.
optionsThe root route, clock and id source, as for the other overload.
Exceptions
InvalidOperationExceptionNo generated Bridge is registered for
TViewModel.
Remarks
await using var services = new ServiceCollection().AddRunicViews().BuildServiceProvider();
var shell = new ShellViewModel(new InMemoryNotes());
using var host = RunicWindowTestHost.Create(services, shell);The MVVM adapters receive the logger factory before this call returns, but after the test built the ViewModel, so a ReactiveUI command it created earlier does not report a scheduler that is not Runic's.
CreateWindowAsync
public static Task<RunicTestWindow> CreateWindowAsync<TWindow>(IServiceProvider services, RunicWindowOptions? options = null, RunicWindowTestHostOptions? hostOptions = null, CancellationToken cancellationToken = default) where TWindow : class, IRunicWindow, new() Opens TWindow headless, as a host opens it: in a new scope of services, with its close guard, and with the application's IRunicWindowService bound to the test, so the Windows its ViewModels open, find, activate and open for a result are test Windows too.
Parameters
servicesThe application's root services.
optionsThe Window's options, as for
OpenWindowAsync.hostOptionsThe clock, id source, View locator, model context and logger factory of every Window, and each Window's own
RunicWindowTestHostOptions.Locale.RunicWindowTestHostOptions.ApplicationLocalesets the registeredIRunicApplicationLocaleofservices, a singleton that outlives the Windows; disposing the returned Window sets it back.cancellationTokenCancels opening.
Returns
The open Window.
Exceptions
RunicWindowConfigurationExceptionThe Window's ViewModel or Bridge is not registered.
Remarks
await using var services = new ServiceCollection().AddMyApplication().BuildServiceProvider();
var editor = await RunicWindowTestHost.CreateWindowAsync<EditorWindow>(services);
await editor.Root<EditorViewModel>().SetAsync(x => x.Text, "unsaved");
Assert.False(await editor.RequestUserCloseAsync()); // CanCloseAsync kept it openservices is the application's root provider, built with its own registration method: the Windows' ViewModels, the generated AddRunicViews() and AddRunicWindowService(). Each Window opens in its own scope, which the test host releases when the Window is released. Every Window shares one clock (a FakeTimeProvider unless hostOptions name another) and one id source. Each Window gets a RunicTestWindowPresentation, or the one its scope registers.
Dispose the result to close and release every Window. RunicTestWindow.OpenedWindows lists the Windows opened after it and RunicTestWindow.WaitForOpenedWindowAsync waits for one.