Should I be unit testing my bootloader, and if so, how?

My bootstrapper inherits from UnityBootstrapper, and I tried to unit test it and failed. I wanted to check if the correct modules were added to the ConfigureModuleCatalog method. Should I try unit test this, and if so, how can I test it? I am 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));
    }
}
+4
source share
1 answer

Yes, I would just modify my Bootstrapper a bit to help with 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 make your unit test look 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)
   }
}
+3
source

Source: https://habr.com/ru/post/1617159/


All Articles