Osheca
Osheca

Reputation: 39

Unit Tests with Moq getting an error ""An expression tree may not contain a call or invocation that uses optional arguments""

I am writing Unit Tests with Moq when I suddenly encountered this error message while typing one of my Async methods:

"An expression tree may not contain a call or invocation that uses optional arguments"

However, when I was looking at my existing code, I was calling the code with just 1 argument.

Async Method definition:

public virtual async Task> Async(Guid fileId, MemoryStream stream = null)

        scope.MyMock.Setup(x => x.Async(fileId)).Returns(Task.FromResult(Result.Ok(new ValidationResult())));

Gives me an error: "An expression tree may not contain a call or invocation that uses optional arguments". Please help.

Upvotes: 0

Views: 873

Answers (1)

crgolden
crgolden

Reputation: 4634

You need to pass all arguments, even if they have a default value. In your case, the argument stream has a default value of null, but you still need to pass it.

So you could have this:

scope.MyMock.Setup(x => x.Async(fileId, null)).ReturnsAsync(Result.Ok(new ValidationResult()));

Upvotes: 1

Related Questions