Moq TargetParameterCountException with lambda expression

I have a strange problem: when I use mu repository stub, I get a strange exception:

System.Reflection.TargetParameterCountException

Creating a stub (in the testing 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 with each call to FindAll, this error is thrown :( I am mocking this in many other places, but now I can not find the source of this strange problem :(

+4
source share
1 answer

You missed a couple of brackets after calling AsQueryable :

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

The Returns method has several overloads, and most of them accept Func, and without parentheses, it uses one of these overloads because you did not specify the parameter why it throws an exception.

+13
source

Source: https://habr.com/ru/post/1379536/


All Articles