c# - 测试依赖 MEF 注入(inject)的非导出类

标签 c# silverlight prism mef

我正在使用 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/

相关文章:

c# - 比较阵列之间的距离?

c# - .Net 4.0 和 .NET 4.7.2 标题选择之间 DataGridView 的重大变化

c# - 如何从 nuget 包中排除子目录和内容

.net - WPF 是 Windows 应用程序的选择吗?

WPF Prism如何在一个区域内有重复的 View

c# - 如何从 c# 中的 ViewModelHelper 关闭窗口(XAML)?

c# - 还原后 AMO 数据库对象不刷新

silverlight - Silverlight Toolkit Chart Line Series之颜色选择

c# - 是否有 Silverlight 支持的平面文件数据库?

c# - 如何选择 WPF Prism 应用程序使用的 IoC 容器?