Returnsasync (null) generates a build error when using Moq for unit testing in VS15

When I use ReturnsAsync(null) in the C # unit test method in Visual Studio (with Moq ), I get the error:

"The call is ambiguous between the following methods or properties:

and then a list of ReturnsAsync methods that have different parameters. I understand that this is due to overloading the ReturnsAsync function. However, when I run the same unit test on my peers computer, it works without any errors. Does anyone know why this will happen? Does anyone know how to fix this?

Also, when I create, I get warnings that:

All packages referencing ******** must install the Microsoft.Bcl.Build nuget package.

Could this have any effect?

+6
source share
1 answer

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); 
+8
source

Source: https://habr.com/ru/post/1014057/


All Articles