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 .ThrowsAsync() that will correctly mock exception on async methods #663

Merged
merged 3 commits into from
Feb 27, 2022

Conversation

Socolin
Copy link
Contributor

@Socolin Socolin commented Aug 29, 2021

It should fix #609

@Socolin
Copy link
Contributor Author

Socolin commented Aug 29, 2021

I'm unsure about some part, I had to add #if for net4.5 (no Task.FromException)
Also netstandard < 2.1 and netcoreapp < 2.1 does not have ValueTask

I did use some reflection to find the appropriate Task.FromException / Task.FromException<T> when using .ThrowsAsync<TException>() on a Task<TResult> to avoid to force the users to specify the return type each time (.ThrowsAsync<TResult, TException>()) but I'm not sure about this part. Since I was not able to do the same for ValueTask<T>, if you have any preference on this let me know I can change it.

@dtchepak
Copy link
Member

Thanks a lot for this! It looks great!

I'm playing around with some ideas for reducing the scope of the #if switches. I'll get back to you.

@Socolin
Copy link
Contributor Author

Socolin commented Jan 31, 2022

I've updated the PR with your changes

@dtchepak dtchepak merged commit a6ce8c1 into nsubstitute:main Feb 27, 2022
@dtchepak
Copy link
Member

Thanks @Socolin ! Sorry for the delay in merging and my incomplete PR 🤦

@Socolin
Copy link
Contributor Author

Socolin commented Feb 27, 2022

Thanks @Socolin ! Sorry for the delay in merging and my incomplete PR facepalm

That's great news :) Thanks to have taken time for this.

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.

Mock throwing an exception for async methods
2 participants