Moq TargetParameterCountException with lambda expression

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

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 :(

Answers


You have missed a pair of parenthesis after the AsQueryable call:

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.


Need Your Help

View changing on UITableView update

ios uitableview uicollectionview uipickerview

I have a rather complex form being laid out in a UITableView. This form has some UICollectionView inside a table view cell and also some pickers that show up the same way it does on Calendar app:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.