对具有 MEF 导入值的类进行单元测试
我有一个名为“ViewFactory”的类,该类现在应该提供正确的视图,
它只有一个方法(并且它会增长),我想针对该方法编写单元测试。
该类看起来像这样...
public class ViewFactory
{
[ImportMany(AllowRecomposition=true)]
IEnumerable<ExportFactory<DependencyObject, IViewMetaData>> Views { get; set; }
public DependencyObject GetViewByName(string name)
{
DependencyObject view = null;
try
{
view = Views.Where(v => v.Metadata.ViewName == name).FirstOrDefault().CreateExport().Value;
return view;
}
catch (Exception ex)
{
return view;
}
}
}
我真正想要的是测试我的方法,但不知道该怎么做,因为视图列表是在运行时组成的...
我想测试我是否获得有效的视图姓名 和 我还想测试如果我的名称无效,我是否会得到 null
正确的方法是什么?
I have a a class called "ViewFactory" and this class should deliver the right view
right now it has only one method (and it will grow) which I want to write a unit test against.
the class looks like this...
public class ViewFactory
{
[ImportMany(AllowRecomposition=true)]
IEnumerable<ExportFactory<DependencyObject, IViewMetaData>> Views { get; set; }
public DependencyObject GetViewByName(string name)
{
DependencyObject view = null;
try
{
view = Views.Where(v => v.Metadata.ViewName == name).FirstOrDefault().CreateExport().Value;
return view;
}
catch (Exception ex)
{
return view;
}
}
}
what I do want is to test my method but don't know how to do it because the List of Views is composed on runtime...
I want to test if I get a view for a valid name
and
I also want to test if I get null if I have an invalid name
What would be the right way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将为
ViewFactory
提供一组适合特定测试的ExportFactory<,>
值。不同的测试可能有不同的集合,以允许您测试不同的东西。基本上你是在伪造注入的依赖关系。You would provide your
ViewFactory
with a set ofExportFactory<,>
values suitable for the particular test. Different tests might have different sets, to allow you to test different things. Basically you're faking the injected dependency.这是代码(针对 Silverlight)
Here's the code (for Silverlight)