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

Adding a generic ThrowsAsync method #692

Closed
jehof opened this issue Sep 14, 2018 · 3 comments
Closed

Adding a generic ThrowsAsync method #692

jehof opened this issue Sep 14, 2018 · 3 comments

Comments

@jehof
Copy link

jehof commented Sep 14, 2018

Is it possible to add a generic version of ThrowsAsync like it is available with Throws, so that we could write

mock.Setup(p => p.DoAsync()).ThrowsAsync<InvalidOperationException>();

instead of

mock.Setup(p => p.DoAsync()).ThrowsAsync(new InvalidOperationException());

@stakx
Copy link
Contributor

stakx commented Sep 14, 2018

This would certainly be possible. (Update: See post below.) Before making a decision to implement this however, we'll need to first review #384... otherwise any effort here might ultimately be in vain.

@stakx
Copy link
Contributor

stakx commented Feb 16, 2019

I've taken a closer look today, and it turns out that this isn't as easy as I thought... more likely, it's not possible to implement this in a useful manner.

The problem is that the extension method for mock.ThrowsAsync(exception) is already generic:

https://github.com/moq/moq4/blob/4abde509e4362c16f56b7386d830ea4f4dc7975f/src/Moq/ReturnsExtensions.cs#L73

While we could just have another extension method overload with an additional TException generic parameter, that wouldn't be very practical because you'd always have to specify all generic arguments (e.g. setupOnFooMock.ThrowsAsync<IFoo, SomeException>() instead of just setupOnFooMock.ThrowsAsync<SomeException>()).

So why does the generic argument thingy work with the non-async setup.Throws(exception)? Because that method isn't generic at all. Instead, the generic parameter for the mocked type sits on the containing interface. Unfortunately, adding ThrowsAsync in the same interface isn't an option, because we can only specialize it for Task and ValueTask return values using extension methods.

So unfortunately, I don't think adding the requested ThrowsAsync<TException> overload is worth it because it will result in an impractical API. Because of this, I'm going to close this issue. Sorry for the bad news! 😢

@stakx stakx closed this as completed Feb 16, 2019
@mnivet
Copy link

mnivet commented Oct 23, 2019

You can use Moq.SetupAsync to achieve that, but with a slightly different syntax:

mock.SetupAsync(p => p.DoAsync()).Throws<InvalidOperationException>();

The package provide a SetupAsync extension methods, that then provide similar features than the standard Setup method, but we known from the start that we are in an async context which allow to provide the correct implementations for Task and Task<T>

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

No branches or pull requests

3 participants