使用统一和访问器类进行测试
因此,我需要测试一些具有包含适配器的私有字段的视图模型,该适配器通过视图中的 ICommands/输入/等进行操作,我认为这是一个非常常见的模型,视图公开了一些特定于的公共属性该特定视图是关于什么的,然后适配器被隐藏在私有字段中。现在我的问题是测试。我正在使用 Unity 注册该适配器的实例,然后每个视图模型当然都会获得相同的实例,但我无法测试它,因为它是私有的,所以我创建了一个访问器类,但我不知道如何创建和它的实例。也许一些代码将有助于解释我想要做什么。现在请记住,我是测试新手,因此如果我对某些事情完全感到困惑,请告诉我。
[TestInitialize]
public void Initialize()
{
container = new UnityContainer();
container.RegisterType<IEventAggregator, EventAggregator>();
container.RegisterType<IRegionManager, RegionManager>();
//Notice that I am not calling registertype but later on I call container.Resolve asking for
//CreateRgaViewModel - this is because I don't have to regiter the types / instances of
//whatever I am directly asking for but I do have to register anything it depends on.
//container.RegisterType<CreateRgaViewModel>();
var adapter = new RgaWizardAdapter(container);
//So we don't want to get any data at this point because this is not an integrration test
//adapter.InitializeRga(873632);
container.RegisterInstance<IRgaWizardAdapter>("RgaAdapterInstance", adapter);
var appCommands = new ApplicationCommands(container);
container.RegisterInstance<IApplicationCommands>(appCommands);
}
[TestMethod]
public void CanCreate_CreateRgaViewModelAndGetNamedInstanceOfRgaDocument()
{
try
{
//this fails
//CreateRgaViewModel_Accessor createRgaViewModel = container.Resolve<CreateRgaViewModel_Accessor>();
//this works
CreateRgaViewModel createRgaViewModel = container.Resolve<CreateRgaViewModel>();
Assert.IsNotNull(createRgaViewModel, "CreateRgaViewModel was null");
}
catch (Exception ex)
{
}
}
我的问题是我发现了很多关于测试的东西,但它似乎是特定于使用 silverlight 的。我不是在创建 silverlight 应用程序,这是桌面 WPF / MVVM 应用程序。
感谢您的帮助
So I need to test some view models that have a private field containing an adapter, this adapter gets manipulated via ICommands / input / etc from the views, I think this is a pretty common model, the view exposes some public properties that are specific to what that particular view is all about and then then the adapter is concelled in a private field. Now my problem is testing. I am using Unity to register an instance of that adapter and then each view model gets that same instance of course but I can't test it cause it is private so I created an accessor class but I can't figure out how to create and instance of it. Maybe some code will help explain what I am trying to do. Now keep in mind I am new to testing so if I am totally confused about something please let me know.
[TestInitialize]
public void Initialize()
{
container = new UnityContainer();
container.RegisterType<IEventAggregator, EventAggregator>();
container.RegisterType<IRegionManager, RegionManager>();
//Notice that I am not calling registertype but later on I call container.Resolve asking for
//CreateRgaViewModel - this is because I don't have to regiter the types / instances of
//whatever I am directly asking for but I do have to register anything it depends on.
//container.RegisterType<CreateRgaViewModel>();
var adapter = new RgaWizardAdapter(container);
//So we don't want to get any data at this point because this is not an integrration test
//adapter.InitializeRga(873632);
container.RegisterInstance<IRgaWizardAdapter>("RgaAdapterInstance", adapter);
var appCommands = new ApplicationCommands(container);
container.RegisterInstance<IApplicationCommands>(appCommands);
}
[TestMethod]
public void CanCreate_CreateRgaViewModelAndGetNamedInstanceOfRgaDocument()
{
try
{
//this fails
//CreateRgaViewModel_Accessor createRgaViewModel = container.Resolve<CreateRgaViewModel_Accessor>();
//this works
CreateRgaViewModel createRgaViewModel = container.Resolve<CreateRgaViewModel>();
Assert.IsNotNull(createRgaViewModel, "CreateRgaViewModel was null");
}
catch (Exception ex)
{
}
}
My problem is that I find a lot of stuff on testing but it seems to be specific to using silverlight. I am not creating a silverlight app, this is desktop WPF / MVVM app.
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论