C# 使用unity和accessor类进行测试
所以我需要测试一些视图模型,这些视图模型有一个包含适配器的私有字段,这个适配器通过ICommands/input/etc从视图中进行操作,我认为这是一个非常常见的模型,该视图公开一些特定于该特定视图的公共属性,然后将适配器集中在一个私有字段中。现在我的问题是测试。我正在使用Unity注册适配器的一个实例,然后每个视图模型都会得到相同的实例,当然,我不能测试它,因为它是私有的,所以我创建了一个访问器类,但我不知道如何创建它的实例。也许一些代码可以帮助解释我想做什么。现在请记住,我是测试新手,所以如果我对某些事情完全困惑,请让我知道C# 使用unity和accessor类进行测试,c#,visual-studio-2010,testing,mvvm,unity-container,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)
{
}
}
[测试初始化]
公共无效初始化()
{
容器=新的UnityContainer();
container.RegisterType();
container.RegisterType();
//请注意,我不是在调用registertype,而是在稍后调用container.Resolve请求
//CreateRgaViewModel-这是因为我不必注册
//无论我直接要求什么,但我必须注册它所依赖的任何东西。
//container.RegisterType();
var适配器=新适配器(容器);
//所以我们现在不想得到任何数据,因为这不是一个积分测试
//适配器初始化器(873632);
容器.RegisterInstance(“RGAApterInstance”,适配器);
var appCommands=新的应用程序命令(容器);
container.RegisterInstance(appCommands);
}
[测试方法]
public void可以创建_creatergaviewmodel和getnamedinstanceofrgadocument()
{
尝试
{
//这失败了
//CreateRgaViewModel访问器CreateRgaViewModel=container.Resolve();
//这很有效
CreateRgaViewModel CreateRgaViewModel=container.Resolve();
IsNotNull(createRgaViewModel,“createRgaViewModel为null”);
}
捕获(例外情况除外)
{
}
}
我的问题是,我在测试中发现了很多东西,但它似乎是特定于使用silverlight的。我不是在创建silverlight应用程序,这是桌面WPF/MVVM应用程序
谢谢你的帮助