My bootstrapper inherits from UnityBootstrapper and I was attempting to unit test it and failed. I wanted to test that the correct modules get added in the ConfigureModuleCatalog method. Should I be trying to unit test this and if so how could I test it? I'm using .NET 4.5.1 and Prism 6
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)Shell;
Application.Current.MainWindow.Show();
}
protected override void ConfigureModuleCatalog()
{
ModuleCatalog moduleCatalog = (ModuleCatalog)ModuleCatalog;
moduleCatalog.AddModule(typeof(MainShellModule));
}
}
Yeah, I would just modify your Bootstrapper slightly to help with the testing:
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
base.InitializeShell();
Window app_window = Shell as Window;
if((app_window != null) && (Application.Current != null))
{
Application.Current.MainWindow = app_window;
Application.Current.MainWindow.Show();
}
}
protected override void ConfigureModuleCatalog()
{
ModuleCatalog moduleCatalog = (ModuleCatalog)ModuleCatalog;
moduleCatalog.AddModule(typeof(MainShellModule));
}
}
Then you could have your unit test look something like this:
[TestFixture, RequiresSTA]
public class BootstrapperTest
{
// Declare private variables here
Bootstrapper b;
/// <summary>
/// This gets called once at the start of the 'TestFixture' attributed
/// class. You can create objects needed for the test here
/// </summary>
[TestFixtureSetUp]
public void FixtureSetup()
{
b = new Bootstrapper();
b.Run();
}
/// <summary>
/// Assert container is created
/// </summary>
[Test]
public void ShellInitialization()
{
Assert.That(b.Container, Is.Not.Null);
}
/// <summary>
/// Assert module catalog created types
/// </summary>
[Test]
public void ShellModuleCatalog()
{
IModuleCatalog mod = ServiceLocator.Current.TryResolve<IModuleCatalog>();
Assert.That(mod, Is.Not.Null);
// Check that all of your modules have been created (based on mod.Modules)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With