c# - How to test an async exception handler -


i have method task<details> getdetails(int number);.

it's called async mvc controller:

        var details = _provider.getdetails(input);         details details = null;         try         {             details = await getdetails;         }         catch (exception ex)         {             // code needs testing         } 

attempt test in custom, non-async testing framework. getdetails method stubbed, using rhinomocks: provider.stub(provider => provider.getdetails(input) .return(new task<details>(delegate { throw new exception {}; }));

the result deadlock of unit test, runs forever. advice on how fix this?

the cause of deadlock you're creating new task without starting it.

for exceptions, it's better use taskcompletionsource:

var tcs = new taskcompletionsource<details>(); tcs.trysetexception(new exception()); provider.stub(...).return(tcs.task); 

however, @servy says, won't far in testing asynchronous methods without unit test framework explicitly support async. it's doable inefficient , requires ceremony in every test method.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -