Skip to content

Conversation

@Stedoss
Copy link
Contributor

@Stedoss Stedoss commented Jun 18, 2022

Currently, you are not able to ReturnsNull() on nullable value types (eg. int? and Guid?), and when wrapped in tasks they will throw a nullref, such as:

var foo = Substitute.For<IFoo>();
foo.Bar().ReturnsNull();
Console.WriteLine(await foo.Bar());

public interface IFoo
{
    public Task<int?> Bar();
}

This PR should help fix this by extending the extension methods to handle nullable value types, along with wrapping them in Task and ValueTask types when necessary.

I have written unit tests such that it tests for nullability on the newly added functionality, please let me know if more thorough tests are needed.

Note: ReturnsNull() will still throw a nullref if the value type inside of a Task is NOT nullable.

Thank you!

@dtchepak dtchepak merged commit 5fa96da into nsubstitute:main Jul 10, 2022
@dtchepak
Copy link
Member

Thanks @Stedoss !

@dtchepak
Copy link
Member

Released in 4.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants