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
Return Content with Ihttpactionresult for Non-Ok Response
Xamarin Project Not Running, Assembly Not Found
Lock-Free Multi-Threading Is for Real Threading Experts
How am I Misusing the Null-Coalescing Operator? Is This Evaluating "Null" Correctly
If My Interface Must Return Task How to Have a No-Operation Implementation
Argumentoutofrangeexception on Initialized List
Ocr with the Tesseract Interface
How to Determine a Session Variable Is Null or Empty in C#
Wix Service Installer Overrides Service Installer Settings
Calling Async Methods from Non-Async Code
What's the Fastest Way to Bulk Insert a Lot of Data in SQL Server (C# Client)
How to Make a Combobox Non-Editable in .Net
Are There Good Reasons Not to Use an Orm
How to Fix Wpf Error: "Program Does Not Contain a Static 'Main' Method Suitable for an Entry Point"