Open FNERNST opened 1 year ago
I can confirm this issue:
The issue is that Task.FromResult(1);
will create a Task<int>
and not a Task<int?>
Hi, @FNERNST, @304NotModified
I don't think this is an NSubstitute
problem. And I don't think it is a problem at all.
By using Task.FromResult
and passing 1
there, you are telling it that you intend to use the int
type. It's impossible for the compiler to get a value with type int?
by passing just the value 1
. You need to ask the compiler to use the int?
type explicitly.
Try something like this:
test.TestAsync().Returns(Task.FromResult<int?>(1));
Screenshot:
Why do you even write Task.FromResult
? You can simply write test.TestAsync().Returns(1);
and NSubstitute will wrap it in a task for you. This even works with the nullable example.
Describe the bug When using the Returns extension method with a Task that may contain a nullable value, the method is ambiguous. The same happens for non primitive nullable types. See the example below which results in a compilation error.
To Reproduce
Expected behaviour The correct Returns method is called.
Environment: