An Expression Tree May Not Contain a Call or Invocation That Uses Optional Arguments

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

The underlying expression tree API does not support optional arguments.

For IL-compiled code the C# compiler inserts the default values at compile time (hard-coded), because the CLR does not support calling methods with optional arguments either when the arguments are not provided explicitly.

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

The mock expects the entire member definition to be configured/setup

Expect the options parameter using It.IsAny<CancellationToken>()

public async Task Signup(UserTO user) {

var req = new SignUpRequest() {

};
_cognito.Setup(m =>
m.SignUpAsync(It.IsAny<SignUpRequest>(), It.IsAny<CancellationToken>())
)
.ReturnsAsync(() => new SignUpResponse());

//...
}

Moq throws exceptions stating that an expression tree may not contain a call or invocation that uses optional arguments

You need to fill in all the optional arguments when you setup the mock. For example:

this.geographyRepository.Setup(x => x.GetAsync(
It.IsAny<Expression<Func<Geography, bool>>>(),
It.IsAny<Func<IQueryable<Geography>, IOrderedQueryable<Geography>>>(),
It.IsAny<Expression<Func<Geography, object>>[]>())).ReturnsAsync(geographyObjList);

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

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()));

.Net Core Moq: An expression tree may not contain a call or invocation that uses optional arguments

moqDb is intance of Mock:

moqDb
.Setup(_ => _.GetContext().ScanAsync<MyModel>

In this part of the code you trying to "call" a method ScanAsync on not already set up GetContext(). To solve this you have to Setup return value for GetContext() before you tried to Setup ScanAsync()

C# forcing to include optional parameters?

Referring to:
https://github.com/Dresel/RouteLocalization/issues/6 and An expression tree may not contain a call or invocation that uses optional arguments

you cannot use it with default arguments



Related Topics



Leave a reply



Submit