Note that there are some explanatory texts on larger screens.

plurals
  1. POIntegration Testing a ViewModel that calls WCF Services asynchronously in a WPF MVVM Application
    text
    copied!<p>The Silverlight Toolkit contains Unit Testing Functionality that allows to test classes such as the ViewModels in a MVVM application that invoke remote Services asynchronously.</p> <p>I would like to be able to perform my ViewModel Integration Tests against the actual services instead of mocked instances. </p> <p>Is there any support for asynchronous Unit/Integration Testing for WPF Applications?</p> <p><strong>Update:</strong></p> <p>At the end of the day my solution combined the suggestions of ktutnik and Alex Paven. I wrote a tiny helper class that adds some syntactic sugar:</p> <pre><code>public static class AsyncMethod { public delegate void AsyncMethodCallback(AsyncMethodContext ctx); public static void Call(AsyncMethodCallback cb) { // create the sync object and make it available via TLS using (var syncObject = new AutoResetEvent(false)) { // call the decorated method cb(new AsyncMethodContext(syncObject)); } } } /// &lt;summary&gt;Asnc Method Callback Synchronization Context&lt;/summary&gt; public class AsyncMethodContext { public AsyncMethodContext(EventWaitHandle syncObject) { this.syncObject = syncObject; } private readonly EventWaitHandle syncObject; /// &lt;summary&gt; /// Waits for completion. /// &lt;/summary&gt; public void WaitForCompletion() { syncObject.WaitOne(); } /// &lt;summary&gt; /// Signals the current operation as complete /// &lt;/summary&gt; public void Complete() { syncObject.Set(); } } </code></pre> <p>Here's a sample test case combined with the utilization of the Microsoft Rx Extensions:</p> <pre><code>[TestMethod] public void TestGuestLogin() { AsyncMethod.Call((ctx) =&gt; { var vm = ServiceLocator.Get&lt;LoginDialogViewModel&gt;(); // setup VM data vm.Username = "guest"; vm.Password = "guest"; vm.AutoLogin = false; GenericInfoEventArgs&lt;LoginDialogViewModel.LoginRequestResult&gt; loginResult = null; // pre-flight check Assert.IsTrue(vm.LoginCmd.CanExecute(null)); // create Observable for the VM's LoginRequestComplete event var loginEvent = Observable.FromEvent&lt;GenericInfoEventArgs&lt;LoginDialogViewModel.LoginRequestResult&gt;&gt;(vm, "LoginRequestComplete").Do((e) =&gt; { Debug.WriteLine(e.ToString()); }); // subscribe to it var loginEventSubscription = loginEvent.Subscribe((e) =&gt; { loginResult = e.EventArgs; // test complete ctx.Complete(); }); // set things in motion using (loginEventSubscription) { vm.LoginCmd.Execute(null); ctx.WaitForCompletion(); Assert.IsTrue(loginResult.Info.Success, "Login was not successful"); } }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload