2

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?

James
  • 523
  • 1
  • 4
  • 20
  • Does this answer your question? [Setup Method With Params Array](https://stackoverflow.com/questions/7968052/setup-method-with-params-array) Try to remove array declaration and use something like `It.IsAny>>()` – Pavel Anikhouski Apr 10 '20 at 21:23
  • @PavelAnikhouski no, but I just found my problem after looking at some other samples. – James Apr 10 '20 at 21:25

1 Answers1

3

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
            });
James
  • 523
  • 1
  • 4
  • 20