简体   繁体   中英

How to handle exceptions in async void methods with NUnit

I have some tests that run through the workflow of my WPF program. I do the normal MVVM approach which is binding buttons on the view to commands on the view model, which then handles the event. The way my tests test my workflow is then by directly executing the commands on the view model. This roughly translates to look something like:

[Test]
public void Test()
{
    var vm = new ViewModel();
    vm.AcceptCommand.Execute();
    Assert.IsTrue(stuff);
}

All of this works well, except for the fact that the code in the viewmodel that handles the command ends up being an async void method since this just becomes an event handler. If an exception gets thrown here, nunit does not show a failing test because it doesn't "see" this exception in the background thread.

My question is: is there a way to get NUnit to handle these background exceptions?

If it's possible then refactor your method into 2 methods. First one should return Task and it's the one that is testable. The other should call and await first method. This hint is taken from Concurrency in C# Cookbook by Stephen Cleary. This is the preferred method. This book also mentions AsyncContext class from AsyncEx library which allows to test async void methods.

AsyncContext.Run(() =>
 { 
     // put your code here
 })

Quote from the same book: The AsyncContext type will wait until all asynchronous operations complete (including async void methods) and will propagate exceptions that they raise.

Take a look here for similar discussion: Await a Async Void method call for unit testing

There is a separate method in NUnit to verify exceptions in async methods:

var exception = Assert.ThrowsAsync<NotImplementedException>(() => vm.AcceptCommand.Execute());

or vice versa:

Assert.DoesNotThrowAsync(() => vm.AcceptCommand.Execute());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM