There are two methods for extending ReturnsAsync
in the Moq ReturnsExtensions
class. They have the following options:
(this IReturns<TMock, Task<TResult>> mock, TResult value) (this IReturns<TMock, Task<TResult>> mock, Func<TResult> valueFunction)
As you can see, one accepts a value that should be returned by the task, and the other accepts a delegate that will return a value. When you pass null
, the compiler does not know if it evaluates or delegates. This is not the case when the task parameter is a value type (for example, int). Since it cannot be null, and the compiler understands that null is a delegate. This is probably the case with your computer colleague.
To fix this error, you need to help the compiler choose the correct method overload - specify null in the type of the result of the task (for example, a string):
RetursAsync((string)null)
Or you can pass a value that is null
string s = null; ... ReturnsAsync(s);
source share