Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Moq函数参数_C#_Unit Testing_Moq_Func - Fatal编程技术网

C# Moq函数参数

C# Moq函数参数,c#,unit-testing,moq,func,C#,Unit Testing,Moq,Func,我有一个使用Func参数的单元测试,我似乎无法使用MOQ。看看StackOverflow上Func参数的其他示例,我认为这是应该起作用的: 要测试的代码: public class PatternManager : IPatternManager { private readonly ICacheManager _cacheManager; private readonly IDataRepo _data; public PatternManager(ICacheMan

我有一个使用Func参数的单元测试,我似乎无法使用MOQ。看看StackOverflow上Func参数的其他示例,我认为这是应该起作用的:

要测试的代码:

public class PatternManager : IPatternManager
{
    private readonly ICacheManager _cacheManager;
    private readonly IDataRepo _data;

    public PatternManager(ICacheManager cacheManager, IDataRepo dataRepo)
    {
        _cacheManager = cacheManager;
        _data = dataRepo;
    }

    public List<Pattern> GetAllPatterns()
    {
        var allPatterns = _cacheManager.Get(() => _data.GetAllPatterns(), nameof(_data.GetAllPatterns));

        return allPatterns;
    }

    public Pattern GetBestPattern(int[] ids)
    {
        var patternList = GetAllPatterns().Where(w => w.PatternId > 1);

        ... More code...
    }
    ...
}


public interface ICacheManager
{
    T Get<T>(Func<T> GetMethodIfNotCached, string cacheName = null, int? cacheDurationInSeconds = null, string cachePath = null);

}
公共类模式管理器:IPatternManager
{
专用只读ICacheManager(U cacheManager);
私有只读IDataRepo_数据;
公共模式管理器(ICacheManager、IDataRepo dataRepo)
{
_cacheManager=cacheManager;
_数据=数据报告;
}
公共列表GetAllPatterns()
{
var allPatterns=_cacheManager.Get(()=>_data.GetAllPatterns(),name of(_data.GetAllPatterns));
返回所有模式;
}
公共模式GetBestPattern(int[]id)
{
var patternList=GetAllPatterns(),其中(w=>w.PatternId>1);
…更多代码。。。
}
...
}
公共接口ICacheManager
{
T Get(Func GetMethodIfNotCached,string cacheName=null,int?cacheDurationInSeconds=null,string cachePath=null);
}
我得到的不是我期望从GetAllPatterns()得到的模式列表,而是null

[Test]
public void PatternManager_GetBestPattern()
{
    var cacheManager = new Mock<ICacheManager>();
    var dataRepo = new Mock<IDataRepo>();

    dataRepo.Setup(d => d.GetAllPatterns())
                    .Returns(GetPatternList());

    cacheManager.Setup(s => s.Get(It.IsAny<Func<List<Pattern>>>(), "", 100, "")) // Has optional params
                    .Returns(dataRepo.Object.GetAllPatterns());

    patternManager = new PatternManager(cacheManager.Object, dataRepo.Object);

    int[] pattern = { 1, 2, 3};

    var bestPattern = patternManager.GetBestPattern(pattern);

    Assert.AreEqual(1, bestPattern.PatternId);
}

private static List<Pattern> GetPatternList()
{ 
    ... returns a list of Pattern
}
[测试]
公共无效模式管理器\u GetBestPattern()
{
var cacheManager=new Mock();
var dataRepo=new Mock();
dataRepo.Setup(d=>d.GetAllPatterns())
.Returns(GetPatternList());
cacheManager.Setup(s=>s.Get(It.IsAny(),“”,100,“”)//具有可选参数
.Returns(dataRepo.Object.GetAllPatterns());
patternManager=新的patternManager(cacheManager.Object、dataRepo.Object);
int[]模式={1,2,3};
var bestPattern=patternManager.GetBestPattern(pattern);
AreEqual(1,bestPattern.PatternId);
}
私有静态列表GetPatternList()
{ 
…返回模式的列表
}
此设置有什么问题?

模拟在默认情况下返回null,因为该设置与调用模拟成员时实际传递的设置不同

看看在被测试的成员中调用了什么

//...

var allPatterns = _cacheManager.Get(() => _data.GetAllPatterns(), nameof(_data.GetAllPatterns));

//...
这很可能是一个
Func
和字符串
“GetAllPatterns”
,其他可选参数默认为可选默认值

现在看看测试中设置了什么

//...

cacheManager
    .Setup(s => s.Get(It.IsAny<Func<List<Pattern>>>(), "", 100, "")) // Has optional params
    .Returns(dataRepo.Object.GetAllPatterns());

//...

并注意使用委托捕获
返回中的参数。这样就可以像在被测试的成员中一样调用实际函数。

模拟默认为返回null,因为设置与调用模拟成员时实际传递的设置不同。我知道我是如何走错了方向的。谢谢你,恩科西
//...

cacheManager
    .Setup(s => s.Get<List<Pattern>>(It.IsAny<Func<List<Pattern>>>(), It.IsAny<string>(), It.IsAny<int?>(), It.IsAny<string>()))
    .Returns((Func<List<Pattern>> f, string cn, int? cd, string cp) => f()); //Invokes function and returns result

//...