Thursday, March 24, 2016

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

I was recently 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, so what was the problem?


Well, as it turns out, when this method is called as part of a lambda expression as when used with Moq, calling methods with optional arguments is no longer acceptable.

Therefore, this lambda expression DOES NOT WORK:


scope.MyMock.Setup(x => x.GetAsync(myList).GetAwaiter().GetResult()).Returns(myList);

Instead, I had to modify my lambda expression to the following:


scope.MyMock.Setup(x => x.GetAsync(myList,CancellationToken.None).GetAwaiter().GetResult()).Returns(myList);

That was all there was to it!!

No comments:

Post a Comment