The Silverlight Toolkit contains unit testing functionality that allows you to test classes, such as ViewModels in an MVVM application, that invoke remote services asynchronously.
I would like to be able to run my ViewModel integration tests with actual services, not with mocked instances.
Is there Unit / Integration asynchronous testing support for WPF applications?
Update:
At the end of the day, my solution combined the offers of ktutnik and Alex Paven. I wrote a tiny helper class that adds syntactic sugar:
public static class AsyncMethod
{
public delegate void AsyncMethodCallback(AsyncMethodContext ctx);
public static void Call(AsyncMethodCallback cb)
{
using (var syncObject = new AutoResetEvent(false))
{
cb(new AsyncMethodContext(syncObject));
}
}
}
public class AsyncMethodContext
{
public AsyncMethodContext(EventWaitHandle syncObject)
{
this.syncObject = syncObject;
}
private readonly EventWaitHandle syncObject;
public void WaitForCompletion()
{
syncObject.WaitOne();
}
public void Complete()
{
syncObject.Set();
}
}
Here's a test case example combined with using Microsoft Rx Extensions:
[TestMethod]
public void TestGuestLogin()
{
AsyncMethod.Call((ctx) =>
{
var vm = ServiceLocator.Get<LoginDialogViewModel>();
vm.Username = "guest";
vm.Password = "guest";
vm.AutoLogin = false;
GenericInfoEventArgs<LoginDialogViewModel.LoginRequestResult> loginResult = null;
Assert.IsTrue(vm.LoginCmd.CanExecute(null));
var loginEvent = Observable.FromEvent<GenericInfoEventArgs<LoginDialogViewModel.LoginRequestResult>>(vm, "LoginRequestComplete").Do((e) =>
{
Debug.WriteLine(e.ToString());
});
var loginEventSubscription = loginEvent.Subscribe((e) =>
{
loginResult = e.EventArgs;
ctx.Complete();
});
using (loginEventSubscription)
{
vm.LoginCmd.Execute(null);
ctx.WaitForCompletion();
Assert.IsTrue(loginResult.Info.Success, "Login was not successful");
}
});
}