Search code examples
c#xunit

Test a function throwing an Exception in a Task in xunit


I want to test using xunit a function that run a task and throw in a that task For example :

public void doSomething(){
     Task.Run(() =>
            {
                throw new ArgumentNullException();
            });
}

When I want to test this function by doing this :

[Fact]
public void TestIfTheMethodThrow()
{
   Assert.Throws<ArgumentNullException>(() => doSomething()); // should return true but return false                                                                   
}

I want that the Task.Run() finish completely then the assert can be done. anyone have a solution ?


Solution

  • Raising and handling exceptions using TPL (the Tasks library) is slightly different, than the "standard" exception handling. It is meaningful to evaluate only a completed task, so you need to wait for the completion, even if in this case it is an exception.

    Have a look at this MSDN article Exception handling (Task Parallel Library).

    You have two different options:

    • add .Wait() to the Task.Run(...)`

      Task.Run(() =>
             {
                 throw new ArgumentNullException();
             }).Wait();
      
      
      
    • or wait while the task is completed while(!task.IsCompleted) {}

         var task = Task.Run(() =>
                {
                    throw new ArgumentNullException();
                });
        while(!task.IsCompleted) {}
    
    

    The test result should be then as expected - an exception is thrown.

    It could be, that before the Wait your test has passed sporadically - don't be irritated - in this case the task execution was faster than the test check - this is a dangerous source of subtle errors.