简体   繁体   English

我应该对我的引导程序进行单元测试,如果是这样的话?

[英]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. 我的bootstrapper继承自UnityBootstrapper,我试图对它进行单元测试并失败。 I wanted to test that the correct modules get added in the ConfigureModuleCatalog method. 我想测试在ConfigureModuleCatalog方法中添加了正确的模块。 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 我正在使用.NET 4.5.1和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: 是的,我会稍微修改你的Bootstrapper来帮助测试:

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)
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该对这节课进行单元测试吗? - Should I be unit testing this class? 我应该如何在Controller中模拟SignalR HubContext以进行单元测试? - How should I mock SignalR HubContext in Controller for Unit Testing? 如何(我应该)模拟 DocumentClient 以进行 DocumentDb 单元测试? - How to (should I) mock DocumentClient for DocumentDb unit testing? 我应该复制使用模拟的单元测试的副本并将其更改为用于集成测试的真实数据库吗? - Should I make copies of my unit tests that use mocks and change them to a real database for integration testing? 如何构造该类,以便可以进行尽可能多的单元测试? - How can I structure this class so I can do as much unit testing as possible? 我应该在WebApi项目中使用Bootstrapper类吗? - Should I use a Bootstrapper class in a WebApi project? 我应该在单元测试中使用 AutoMapper 吗? - Should I use AutoMapper in my unit tests? 如何在单元测试中模拟控制器上下文,以便我对字符串函数的部分视图有效? - How do I mock controller context in my unit test so that my partial view to string function works? ASP.NET MVC 4单元测试(我应该测试什么) - ASP.NET MVC 4 Unit testing (what i should test) 单元测试 - 我应该分开测试还是进行一次测试? - Unit testing - should I split up tests or have a single test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM