I've slimmed down some code I'm trying to test to the example below
I've followed the advice I found online and carried out the setup below.
My problem is when the test runs it never returns from the Task.WhenAll
I'm confined to .net 4.5.2 so don't have Task.FromException at my disposal.
The code that calls the dependency is:
public async Task<int> CountCustomFields(bool onlyVisible)
{
var countClientCustomField = clientService.CountCustomFieldsAsync(onlyVisible);
try
{
await Task.WhenAll(countClientCustomField);
}
catch (Exception ex)
{
// some loggging etc
}
finally
{
// some tidy up & set the result
}
return result;
}
Mocking is configured in the following test method:
[Test]
public void WhenAsyncMethodThrows_WeShouldCatchTheException()
{
mockClientService.Setup(x => x.CountCustomFieldsAsync(It.IsAny<bool>()))
.Returns(new Task<int>(() => { throw new Exception("err"); }));
Assert.DoesNotThrowAsync(() => sut.CountCustomFields(true));
}
I'm updating the example to include the dependency mentioned in the comments. I think it can be ignored that this is a microservice. In fact it could be any dependency which returns a Task
public interface IClientService
{
Task<int> CountCustomFieldsAsync(bool onlyVisible);
}