NSubstitute icon indicating copy to clipboard operation
NSubstitute copied to clipboard

Ambiguous method call for Task of nullable types

Open FNERNST opened this issue 2 years ago • 3 comments

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

public interface ITest 
{
  Task<int?> TestAsync();
}

var test = Substitute.For<ITest>();
test.TestAsync().Returns(Task.FromResult(1));

Expected behaviour The correct Returns method is called.

Environment:

  • NSubstitute version: 5.0.0
  • NSubstitute.Analyzers version: CSharp 1.0.16
  • Platform: .NET Core 7 on macOS

FNERNST avatar Aug 17 '23 11:08 FNERNST

I can confirm this issue:

image

The issue is that Task.FromResult(1); will create a Task<int> and not a Task<int?>

304NotModified avatar Apr 30 '24 21:04 304NotModified