Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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通用存储库-TARGETPARAMETERCOUNTEXCEPTION_C#_Include_Mocking_Repository_Moq - Fatal编程技术网

C# Moq通用存储库-TARGETPARAMETERCOUNTEXCEPTION

C# Moq通用存储库-TARGETPARAMETERCOUNTEXCEPTION,c#,include,mocking,repository,moq,C#,Include,Mocking,Repository,Moq,我正在模拟一个通用存储库,刚刚在我的检索方法中添加了第二个参数,允许我传递对象属性的include字符串,我对如何模拟这个问题有点困惑,并且得到了一个TargetParameterCountException 如果有人能把我推向正确的方向,那就太好了 接口: IQueryable<T> Retrieve(Expression<Func<T, bool>> predicate); IQueryable<T> Retrieve(Expression&

我正在模拟一个通用存储库,刚刚在我的检索方法中添加了第二个参数,允许我传递对象属性的include字符串,我对如何模拟这个问题有点困惑,并且得到了一个
TargetParameterCountException

如果有人能把我推向正确的方向,那就太好了

接口:

IQueryable<T> Retrieve(Expression<Func<T, bool>> predicate);

IQueryable<T> Retrieve(Expression<Func<T, bool>> predicate, IEnumerable<string> includes);
IQueryable检索(表达式谓词);
IQueryable检索(表达式谓词,IEnumerable包含);
最低起订量:

var mActionRepository=new Mock();
mActionRepository.Setup(m=>m.Retrieve(It.IsAny()))
.返回(可查询的位置);
mActionRepository.Setup(m=>m.Retrieve(It.IsAny(),It.IsAny())
.返回(可查询的位置);

第一个Moq起作用,第二个不起作用。

返回的方法中,您需要将模拟方法的所有参数类型指定为泛型参数

因此,在第二个
返回
调用中缺少
IEnumerable
,这就是为什么会得到
TargetParameterCountException

因此,您的第二个
返回值应如下所示:

mActionRepository.Setup(m=>m.Retrieve(
It.IsAny(),
It.IsAny())
.返回(
(谓词,包括)=>queryable.Where(谓词));

返回
方法中,您需要将模拟方法的所有参数类型指定为泛型参数

因此,在第二个
返回
调用中缺少
IEnumerable
,这就是为什么会得到
TargetParameterCountException

因此,您的第二个
返回值应如下所示:

mActionRepository.Setup(m=>m.Retrieve(
It.IsAny(),
It.IsAny())
.返回(
(谓词,包括)=>queryable.Where(谓词));
var mActionRepository = new Mock<IRepository<ContainerAction>>();
mActionRepository.Setup(m => m.Retrieve(It.IsAny<Expression<Func<ContainerAction, bool>>>()))
    .Returns<Expression<Func<ContainerAction, bool>>>(queryable.Where);

mActionRepository.Setup(m => m.Retrieve(It.IsAny<Expression<Func<ContainerAction, bool>>>(), It.IsAny<IEnumerable<string>>()))
    .Returns<Expression<Func<ContainerAction, bool>>>(queryable.Where);