Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be unit testing my bootstrapper and if so how?

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));
    }
}
like image 595
justin15 Avatar asked Nov 24 '15 01:11

justin15


1 Answers

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)
   }
}
like image 102
SwDevMan81 Avatar answered Oct 28 '22 01:10

SwDevMan81