James
James

Reputation: 553

Error Mocking a method with two parameters

I have a repository that has a method like this.

FindFirstOrDefault<TEntity>(Expression<Func<TEntity, bool>> expression, params Expression<Func<TEntity, object>>[] includes)

My Mock setup looks like this:

_repositoryMock.Setup(moq => moq.FindFirstOrDefault(It.IsAny<Expression<Func<Order, bool>>>(), It.IsAny<Expression<Func<Order, object>>[]>()))

I've also tried this:

_repositoryMock.Setup(moq => moq.FindFirstOrDefault(It.IsAny<Expression<Func<Order, bool>>>(), Array.Empty<Expression<Func<Order, object>>>()))

Previously when I had just one parameter to that FindFirstOrDefault method (the expression parameter) it ran just fine. Now that I have added the includes parameter I get the following error:

Invalid callback. Setup on method with 2 parameter(s) cannot invoke callback with different number of parameters (1).

Why am I getting an error that says I'm only passing 1 parameter when I am passing two?

Upvotes: 2

Views: 304

Answers (1)

James
James

Reputation: 553

So it appears that I just forgot to update the Returns to include the extra parameter.

My entire setup looked like this before:

_repositoryMock.Setup(moq => moq.FindFirstOrDefault(It.IsAny<Expression<Func<Order, bool>>>()))
            .Returns((Expression<Func<Order, bool>> expression =>
            {
                // RETURN LOGIC
            });

With my new parameter I updated to this:

_repositoryMock.Setup(moq => moq.FindFirstOrDefault(It.IsAny<Expression<Func<Order, bool>>>(), Array.Empty<Expression<Func<Order, object>>>()))
            .Returns((Expression<Func<Order, bool>> expression) =>
            {
                // RETURN LOGIC
            });

That caused the error I was getting.

What I really needed was this:

_repositoryMock.Setup(moq => moq.FindFirstOrDefault(It.IsAny<Expression<Func<Order, bool>>>(), Array.Empty<Expression<Func<Order, object>>>()))
            .Returns((Expression<Func<Order, bool>> expression, Expression<Func<Order, object>>[] includes) =>
            {
                // RETURN LOGIC
            });

Upvotes: 3

Related Questions