如何使用起订量来满足MEF进口依存度的单元测试?依存度、如何使用、单元测试、MEF

2023-09-03 04:04:09 作者:局外人

这是我的接口

public interface IWork
{
    string GetIdentifierForItem(Information information);
}

和我的课

public class A : IWork
{
[ImportMany]
public IEnumerable<Lazy<IWindowType, IWindowTypeInfo>> WindowTypes { get; set; }

public string GetIdentifierForItem(Information information)
{
    string identifier = null;
    string name = information.TargetName;

    // Iterating through the Windowtypes 
    // searching the 'Name' and then return its ID  
    foreach (var windowType in WindowTypes)
    {
        if (name == windowType.Metadata.Name)
        {
            identifier = windowType.Metadata.UniqueID;
            break;
        }
    }
    return identifier;
}
}

问题:我想单元测试方法 GetIdentifierForItem

Problem : I want to unit test the method GetIdentifierForItem

下面是我试图做来解决这个问题 -

Here is what I tried doing to solve it -

(1)创建一个模拟慵懒而设置,它需要在属性返回的值变

(1)Create a mock Lazy and set the values that it needs to return on property gets

VAR windowMock =新的模拟&LT;懒&LT; IWindowType,IWindowTypeInfo&GT;&GT;();  windowMock.Setup(富=&GT; foo.Metadata.Name).Returns(数据); windowMock.Setup(富=&GT; foo.Metadata.UniqueID).Returns(someString);

var windowMock = new Mock<Lazy<IWindowType, IWindowTypeInfo>>(); windowMock.Setup(foo => foo.Metadata.Name).Returns("Data"); windowMock.Setup(foo => foo.Metadata.UniqueID).Returns("someString");

(2)创建一个窗口类型列表,上面的嘲笑对象,然后将其设置为创建的对象

(2)Create a window type list and the above mocked object and then set it to the created A object

var WindowTypesList = new List<IWindowType, IWindowTypeInfo>>();
WindowTypesList.Add(windowMock.Object);
A a = new A();
a.WindowTypes = WindowTypesList;

(3)创建信息模拟

(3) Create the information mock

var InfoMock = new Mock<Information>();
InfoMock.Setup(foo => foo.TargetName).Returns("Data");

要把上面所有一起作为单元测试

To put all of the above together as the unit test

[TestMethod]
public void GetIDTest()
{
    var windowMock = new Mock<Lazy<IWindowType, IWindowTypeInfo>>();
    windowMock.Setup(foo => foo.Metadata.Name).Returns("Data");
    windowMock.Setup(foo => foo.Metadata.UniqueID).Returns("someString");

    var WindowTypesList = new List<Lazy<IWindowType, IWindowTypeInfo>>();
    WindowTypesList.Add(windowMock.Object);

    A a = new A();
    a.WindowTypes = WindowTypesList;
    var InfoMock = new Mock<Information>();
    InfoMock.Setup(foo => foo.TargetName).Returns("Data");

    string expected = "someString"; // TODO: Initialize to an appropriate value
    string actual;
    actual = a.GetIdentifierForItem(InfoMock.Object);
    Assert.AreEqual(expected, actual);

}

本单元测试无法执行,并抛出一个异常TargetInvocationException和veiwing细节,它看起来像我做的东西,我不应该这样做。

This unit test fails to execute and throws an exception 'TargetInvocationException' and veiwing the detail, it looks like I am doing something that I should not be doing.

但我不知道是怎么做的其他方式。我看过一些在起订量的快速入门指南的链接。我知道我失去了一些东西。你能帮助我,指导单位怎么测试呢?

But I am not sure how do it other way. I have read some of the links in the Quickstart guide of Moq. I know I am missing something. Can you help me by guiding how to unit test this?

推荐答案

您不必嘲笑延迟&LT; T,TMetadta&GT; 。它具有足够的灵活性与测试工作。相反,模拟了 IWindowTypeInfo

You don't need to Mock the Lazy<T,TMetadta>. It is flexible enough to work with your test. Instead, Mock the IWindowTypeInfo

[TestMethod]
public void GetIDTest()
{
    var windowTypeInfoMock = new Mock<IWindowTypeInfo>();
    windowTypeInfoMock.Setup(foo => foo.Name).Returns("Data");
    windowTypeInfoMock.Setup(foo => foo.UniqueID).Returns("someString");
    var lazyWindow =
         new Lazy<IWindowType, IWindowTypeInfo>(windowTypeInfoMock.Object);

    var WindowTypesList = new List<Lazy<IWindowType, IWindowTypeInfo>>();
    WindowTypesList.Add(lazyWindow);

    var a = new A();
    a.WindowTypes = WindowTypesList;
    var InfoMock = new Mock<Information>();
    InfoMock.Setup(foo => foo.TargetName).Returns("Data");

    string expected = "someString";
    string actual;
    actual = a.GetIdentifierForItem(InfoMock.Object);
    Assert.AreEqual(expected, actual);
}

您测试通过我的机器只用小的修改,你不需要使用的组合容器进行此测试。

Your test passes on my machine with only small modifications, you do not need to use a composition container for this test.