小编bob*_*iya的帖子

如何使用Moq来满足单元测试的MEF导入依赖性?

这是我的界面

public interface IWork
{
    string GetIdentifierForItem(Information information);
}
Run Code Online (Sandbox Code Playgroud)

和我的班级

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;
}
}
Run Code Online (Sandbox Code Playgroud)

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

以下是我试图解决的问题 -

(1)创建一个模拟Lazy并设置它需要在属性获取时返回的值

var …

.net c# unit-testing mef moq

4
推荐指数
1
解决办法
3905
查看次数

标签 统计

.net ×1

c# ×1

mef ×1

moq ×1

unit-testing ×1