Moq Throw async exception in one of tasks in call to Task.WhenAll
As Bruno correctly pointed out, the problem is that the mocked StartAsync
is throwing an exception synchronously, not returning a faulted task.
However, the correct code cannot use new Task
(which will cause a hang, since the task is never started). Instead, use Task.FromException
:
FirstProcessor.Setup(x => x.StartAsync(It.IsAny<TextWriter>())).Returns(
Task.FromException(new Exception("some exception happened."))
);
Task.WhenAll
will call StartAsync
, that will throw.
The exception is thrown on the calling thread. Before a task could be created.
You want StartAsync
to return a Task
:
firstProcessor.Setup(x => x.StartAsync(It.IsAny<TextWriter>())).Returns(new Task(() => { throw new Exception("err"); }));