Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ReturnsNull calls for nullable value types #692

Merged
merged 1 commit into from
Jul 10, 2022

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.

@Stedoss Stedoss deleted the value-type-nulls branch July 10, 2022 22:29
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