C # use moq to throw exceptions from async method

I use the Moq library as a mocking framework with nunit. I am having problems with how to configure my layout to throw an exception from the async method that returns the task.

any help is appreciated

+4
source share
1 answer

Asynchronous methods usually do not throw exceptions directly - they return tasks that ultimately lead to an error. The easiest way to create such a task is to use Task.FromException.

, , , Task.FromException , , . , , " " -, , - , . , :

// Create a non-generic one if you want, too
private static async Task<T> DelayFaultedTask(Exception e)
{
    await Task.Yield();
    throw e;
}

, " ", .

+6

Source: https://habr.com/ru/post/1651161/


All Articles