Wpf 测试UnityBootTrapper实现失败,原因是应用程序.Current为null
测试UnityBootTrapper实现失败,原因是应用程序.Current为null 我正在使用TDD、Prism和MVVM模式开发一个WPF应用程序。当我编写一个测试来验证应用程序启动后引导程序容器是否为null时,测试失败,因为Wpf 测试UnityBootTrapper实现失败,原因是应用程序.Current为null,wpf,unit-testing,mvvm,tdd,xunit,Wpf,Unit Testing,Mvvm,Tdd,Xunit,测试UnityBootTrapper实现失败,原因是应用程序.Current为null 我正在使用TDD、Prism和MVVM模式开发一个WPF应用程序。当我编写一个测试来验证应用程序启动后引导程序容器是否为null时,测试失败,因为InitializeShell方法中的Application.Current为null 公共类引导程序测试 { [事实] public void BootstrapperServiceLocator() { //安排 var sut=new Bootstrapper
InitializeShell
方法中的Application.Current为null
公共类引导程序测试
{
[事实]
public void BootstrapperServiceLocator()
{
//安排
var sut=new Bootstrapper();
//表演
sut.Run();
//断言
Assert.NotNull(sut.Container);
}
}
当我运行应用程序时,一切都按预期工作;只有在运行测试时,我才会得到null异常
公共类引导程序:UnityBootstrapper
{
受保护的覆盖依赖对象CreateShell()
{
返回ServiceLocator.Current.GetInstance();
}
受保护的覆盖无效初始值设置Shell()
{
base.InitializeShell();
Application.Current.MainWindow=(窗口)Shell;
Application.Current.MainWindow.Show();
}
}
我想知道如何在测试执行期间避免此问题。执行空检查,仅在属性不为空时尝试设置属性
protected override void InitializeShell(){
base.InitializeShell();
if(Application.Current!=null){
Application.Current.MainWindow=(窗口)Shell;
Application.Current.MainWindow.Show();
}
}
这样,当测试和
应用程序时。当前的为null
,测试将跳过尝试访问导致NPE的属性的步骤。执行null检查,仅在属性不为null时尝试设置属性。感谢您的建议。我愿意试试。我想知道是否有一种方法可以在不添加空检查的情况下解决此问题。