简体   繁体   English

带有lambda表达式的Moq TargetParameterCountException

[英]Moq TargetParameterCountException with lambda expression

I have strange problem: when I use mu repository stub I get strange exception: 我有一个奇怪的问题:当我使用mu存储库存根时,我得到奇怪的异常:

System.Reflection.TargetParameterCountException System.Reflection.TargetParameterCountException

Creating stub (in a test method): 创建存根(在测试方法中):

var repositoryStub = new Mock<IRepository<User>>();
repositoryStub.Setup(m => m.FindAll(It.IsAny<Expression<Func<User,bool>>>())).Returns(TestGlobals.TestUsers.AsQueryable<User>);

Interface: 接口:

IQueryable<T> FindAll(System.Linq.Expressions.Expression<Func<T, bool>> whereExpression);

And on every call to FindAll throws that error :( I'm mocking in that fashion in many other places, but now I can't find source of that strange problem :( 并且每次调用FindAll都会抛出错误:(我在很多其他地方以那种方式嘲笑,但现在我找不到那个奇怪问题的来源:(

You have missed a pair of parenthesis after the AsQueryable call: 你在AsQueryable调用之后错过了一对括号:

repositoryStub.Setup(m => m.FindAll(It.IsAny<Expression<Func<User,bool>>>())).Returns(TestGlobals.TestUsers.AsQueryable<User>());

The Returns method has multiple overloads and most of them takes a Func and without the parenthesis it uses one of these overloads and because you haven't specified a parameter that's why it throws an exception. Returns方法有多个重载,大多数都使用Func,没有括号,它使用其中一个重载,因为你没有指定一个参数,这就是它抛出异常的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM