我正在使用 MEF 将我的对象组合在一起,但在测试某些未导出到 MEF 容器但依赖于将对象实例注入(inject)到 MEF 属性 setter 中的类时遇到了问题。
例如考虑以下两个 View 模型
public class ViewModelA
{
[Import]
internal IAdmin AdminService
{
get;
private set;
}
public ViewModelA()
{
CompositionInitializer.SatisfyImports(this);
}
//constructor for testing
internal ViewModelA(IAdmin adminService)
{
this.AdminService = adminService;
}
public void DoSomething()
{
this.AdminService.SetCurrentWindow(new ViewModelB());
}
}
public class ViewModelB
{
[Import]
internal IAdmin AdminService
{
get;
private set;
}
[Import]
internal IAnotherService AnotherServiceService
{
get;
private set;
}
public ViewModelB()
{
CompositionInitializer.SatisfyImports(this);
}
public void DoAnotherThing()
{
//Does something with the properties injected via MEF
}
}
这些类未导出到 MEF 容器,因此我依靠调用 CompositionInitializer.SatisfyImports(this)
来强制导入依赖项。
我想为 ViewModelA 创建一个测试,检查对 DoSomething 的调用导致 IAdmin.SetCurrentWindow
方法被 ViewModelB 实例调用一次。为了满足这一点,我为 ViewModelA 创建了一个构造函数重载,它将 IAdmin 作为参数,我还利用 Moq 和 Silverlight 单元测试框架创建了以下测试。
[TestMethod]
public void DoSomethingStandard_CallsSetCurrentWindowWithViewModelB()
{
var adminServiceMock = new Mock<IAdmin>();
var vmA = new ViewModelA(adminServiceMock.Object);
vmA.DoSomething();
adminServiceMock.Verify(ad => ad.SetCurrentWindow(It.IsAny<ViewModelB>()), Times.Exactly((1)));
}
我的问题是,当测试运行对 ViewModelA.DoSomething
的调用时,会实例化一个 ViewModelB,后者又会抛出一个
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
这是因为 ViewModelB 构造函数调用了 CompositionInitializer.SatisfyImports(this)
但在我的测试项目中没有设置 MEF 容器。
是否有关于如何最好地测试这种情况的想法?或者如何重构代码以使其可测试?
最佳答案
我认为您唯一可以做的就是将您的 [Import]ed 属性更改为使用公共(public) setter 公开并直接设置实例。您不应该担心单元测试中的 MEF。
这thread对此进行了更深入的探讨。
关于c# - 测试依赖 MEF 注入(inject)的非导出类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804146/