使用统一和访问器类进行测试

发布于 2024-10-14 03:47:32 字数 1799 浏览 1 评论 0原文

因此,我需要测试一些具有包含适配器的私有字段的视图模型,该适配器通过视图中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文