CS0854: Moq setup fails for IConnectionMultiplexer.GetDatabase() due to optional parameters
02:27 30 Mar 2026

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 inside Setup(...)

asp.net-core moq xunit stackexchange.redis