This (n)unit test fails:
[Test]
public void FooTest()
{
var foo = new Foo();
Assert.That(() => foo.DoSomething(), Throws.InstanceOf<Exception>());
}
This is the class under test:
public class Foo : IFoo
{
public async void DoSomething()
{
await DoSomethingAsync();
}
private async Task DoSomethingAsync()
{
await Task.Run(() =>
{
Thread.Sleep(500);
throw new Exception("exception");
});
}
}
If I change DoSomething this:
public void DoSomething()
{
DoSomethingAsync().Wait();
}
Then the test will pass. Is there any way I can avoid having to do that?
Note: I cannot change the signature of DoSomething to public async Task DoSomething(), as I cannot change the IFoo interface, as that comes from a 3rd party library.
Task.Wait(). If the interface can't change that's going to be a problem. Doing async over sync is generally a bad idea.async voidlike this, unless you don't care about the result of .DoSomethingAsyncand how exceptions are thrown insideasync void.