gpt4 book ai didi

c# - 如何为 Prism 7 应用创建 Spec-Flow 测试?

转载 作者:行者123 更新时间:2023-11-28 21:36:38 29 4
gpt4 key购买 nike

我正在使用 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com