使用unity和accessor类进行测试

时间:2011-01-24 14:33:43

标签: c# visual-studio-2010 testing mvvm unity-container

所以我需要测试一些具有包含适配器的私有字段的视图模型,这个适配器通过视图中的ICommands / input / etc进行操作,我认为这是一个非常常见的模型,该视图公开了一些公共属性特定于特定视图的所有内容然后适配器被隐藏在私有字段中。现在我的问题是测试。我使用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应用程序。

感谢您的帮助

0 个答案:

没有答案