Using NSubstitute, how do you mock an exception being thrown in a method returning a Task?
Let's say our method signature looks something like this:
Task<List<object>> GetAllAsync();
Here's how NSubstitute docs say to mock throwing exceptions for non-void return types. But this doesn't compile :(
myService.GetAllAsync().Returns(x => { throw new Exception(); });
So how do you accomplish this?
async
method throwsMyCustomException
for example, anAggregationException
will be received if you use.Result
. If you use.GetAwaiter().GetResult()
, then you'll get the realMyCustomException
. Your way would always throwsMyCustomException
, regardless of how it's being used. – Lacrimator