I am writing unit tests in ASP.NET Core using xUnit and Moq, and mocking StackExchange.Redis.
While setting up a mock for IConnectionMultiplexer.GetDatabase(), I get the following compiler error:
CS0854: An expression tree may not contain a call or invocation that uses optional arguments
The error occurs on this line:
mockConn.Setup(c => c.GetDatabase()).Returns(mockDb.Object);
Why this error occurs
The method IConnectionMultiplexer.GetDatabase() is defined as:
IDatabase GetDatabase(int db = -1, object asyncState = null);
This method has optional parameters.
Moq uses expression trees
Expression trees do NOT support optional arguments
Calling
GetDatabase()without explicitly passing all parameters is not allowed insideSetup(...)