I have a method that looks like this:
private async void DoStuff(long idToLookUp)
{
IOrder order = await orderService.LookUpIdAsync(idToLookUp);
You can use an AutoResetEvent to halt the test method until the async call completes:
[TestMethod()]
public void Async_Test()
{
TypeToTest target = new TypeToTest();
AutoResetEvent AsyncCallComplete = new AutoResetEvent(false);
SuccessResponse SuccessResult = null;
Exception FailureResult = null;
target.AsyncMethodToTest(
(SuccessResponse response) =>
{
SuccessResult = response;
AsyncCallComplete.Set();
},
(Exception ex) =>
{
FailureResult = ex;
AsyncCallComplete.Set();
}
);
// Wait until either async results signal completion.
AsyncCallComplete.WaitOne();
Assert.AreEqual(null, FailureResult);
}
An async void
method is essentially a "fire and forget" method. There is no means of getting back a completion event (without an external event, etc).
If you need to unit test this, I would recommend making it an async Task
method instead. You can then call Wait()
on the results, which will notify you when the method completes.
However, this test method as written would still not work, as you're not actually testing DoStuff
directly, but rather testing a DelegateCommand
which wraps it. You would need to test this method directly.