[英]Moq generic Repository - TARGETPARAMETERCOUNTEXCEPTION
I am Mocking a generic repository and have just added a second argument to my Retrieve method allowing me to pass include strings for object properties, I'm a bit stuck on how to Mock this and am getting a TargetParameterCountException
. 我正在模拟通用存储库,并且刚刚在Retrieve方法中添加了第二个参数,从而允许我传递对象属性的包含字符串,但我对如何模拟该特性有些困惑,并且正在获取
TargetParameterCountException
。
If anyone could nudge me in the right direction, that would be great. 如果有人能朝着正确的方向推动我,那将很棒。
Interface: 接口:
IQueryable<T> Retrieve(Expression<Func<T, bool>> predicate);
IQueryable<T> Retrieve(Expression<Func<T, bool>> predicate, IEnumerable<string> includes);
Moq: 起订量:
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);
The first Moq worked, the second doesn't. 最小起订量起作用,第二个不起作用。
In the Returns
method you need to specify all the argument types of the mocked method as generic arguments. 在
Returns
方法中,您需要将模拟方法的所有参数类型指定为通用参数。
So you are missing the IEnumerable<string>
in your second Returns
call that is why you get the TargetParameterCountException
. 因此,您在第二个
Returns
调用中缺少IEnumerable<string>
,这就是为什么您获得TargetParameterCountException
。
So your second Returns
should look like this: 因此,您的第二个
Returns
应如下所示:
mActionRepository.Setup(m => m.Retrieve(
It.IsAny<Expression<Func<ContainerAction, bool>>>(),
It.IsAny<IEnumerable<string>>()))
.Returns<Expression<Func<ContainerAction, bool>>, IEnumerable<string>>(
(predicate, includes) => queryable.Where(predicate));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.