作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Prism 7 创建一个新的 Windows 桌面应用程序,我从替换 Bootstrap 的新 PrismApplication
基类开始。在我创建 (specflow-) 测试之前,一切都很好。
我习惯于在测试应用程序初始化期间重新使用原始 Bootstrap ,只是在之后修改注册。转换为新的 Application
派生系统如下所示:
internal partial class App
{
protected override IContainerExtension CreateContainerExtension()
{
var containerExtension = new Prism.Unity.Ioc.UnityContainerExtension();
containerExtension.Instance.AddExtension( new LogExtension() );
return containerExtension;
}
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes( IContainerRegistry containerRegistry )
{
containerRegistry.Register<IConfiguration, AppSettingsConfiguration>();
containerRegistry.Register<IWindowsInterface, WindowsInterface>();
// ... a lot of registrations removed here ...
}
}
还有一个派生的测试应用程序,它除了创建 shell 之外什么都做:
private class MyApp : App
{
protected override Window CreateShell()
{
return null;
}
}
包装在 BeforeScenario
Hook 中以初始化测试应用程序:
[BeforeScenario]
public void InitializeApp()
{
var app = new MyApp();
app.Initialize();
var containerRegistry = (IContainerRegistry)app.Container;
containerRegistry.RegisterSingleton<TestWindowsInterface>();
containerRegistry.Register<IWindowsInterface,TestWindowsInterface>();
// ... some registration overrides removed here ...
_objectContainer.RegisterInstanceAs<App>( app );
}
还有一个创建主窗口的步骤(CreateShell
替换):
[When( @"I start the software" )]
public void WhenIStartTheSoftware()
{
_container.RegisterInstanceAs( _container.Resolve<App>().Container.Resolve<MainWindowViewModel>() );
}
到目前为止,一切顺利,这很有效。但只要你只有一种情况。第二种情况一开始,我们就得到一个异常:
Cannot create more than one System.Windows.Application instance in the same AppDomain.
在过去,这不是问题,因为 Bootstrapper
只是一个常规类,而不是强制成为单例的 PrismApplication
由框架。
当然,我可以将整个注册内容移动到一个常规类中,并使用它来初始化测试应用程序,但这实际上意味着在 Prism 应用程序
。使用经典的 Bootstrapper
对我来说更有意义,但它将在未来的版本中被删除(因为它今天被标记为已过时)。
最佳答案
您将能够使用从 MarshalByRefObject
派生的类创建 Application
类的多个实例:
public class AppDomainWrapper : MarshalByRefObject
{
public void DoSomething()
{
var app = new MyApp();
app.Initialize();
...
app.Shutdown();
}
}
示例用法:
AppDomain appDomain = AppDomain.CreateDomain("AppDomain");
AppDomainWrapper app = appDomain.CreateInstanceAndUnwrap(typeof(AppDomainWrapper).Assembly.FullName, typeof(AppDomainWrapper).FullName) as AppDomainWrapper;
app.DoSomething();
AppDomain.Unload(appDomain);
关于c# - 如何为 Prism 7 应用创建 Spec-Flow 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58120554/
我是一名优秀的程序员,十分优秀!