我应该对我的引导程序进行单元测试吗?如果是,如何进行?
Should I be unit testing my bootstrapper and if so how?
我的引导程序继承自 UnityBootstrapper,我试图对其进行单元测试但失败了。我想测试是否在 ConfigureModuleCatalog 方法中添加了正确的模块。我应该尝试对此进行单元测试吗?如果是的话,我该如何测试呢?我正在使用 .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));
}
}
是的,我会稍微修改您的 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));
}
}
然后你可以让你的单元测试看起来像这样:
[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)
}
}
我的引导程序继承自 UnityBootstrapper,我试图对其进行单元测试但失败了。我想测试是否在 ConfigureModuleCatalog 方法中添加了正确的模块。我应该尝试对此进行单元测试吗?如果是的话,我该如何测试呢?我正在使用 .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));
}
}
是的,我会稍微修改您的 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));
}
}
然后你可以让你的单元测试看起来像这样:
[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)
}
}