【问题标题】:Testing non exported classes which rely on MEF injection测试依赖 MEF 注入的非导出类
【发布时间】:2012-12-10 15:39:35
【问题描述】:

我正在使用 MEF 将我的对象组合在一起,并且在测试一些未导出到 MEF 容器但依赖于 MEF 将对象实例注入属性设置器的类时遇到了问题。

例如考虑以下两个视图模型

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 的调用是否会导致使用 ViewModelB 的实例调用一次 IAdmin.SetCurrentWindow 方法。为了满足这一点,我为 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:无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。

这是因为 ViewModelB 构造函数调用了CompositionInitializer.SatisfyImports(this),但在我的测试项目中没有设置 MEF 容器。

对于如何最好地测试这种场景有什么想法吗?或者如何重组代码以使其可测试?

【问题讨论】:

    标签: c# silverlight prism mef


    【解决方案1】:

    我认为您唯一能做的就是将您的 [Import] ed 属性更改为使用公共设置器公开并直接设置实例。您不必担心单元测试中的 MEF。

    这个thread 更深入地介绍了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-27
      • 2018-01-27
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      相关资源
      最近更新 更多