Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the WPF Dispatcher in unit tests

I'm having trouble getting the Dispatcher to run a delegate I'm passing to it when unit testing. Everything works fine when I'm running the program, but, during a unit test the following code will not run:

this.Dispatcher.BeginInvoke(new ThreadStart(delegate {     this.Users.Clear();      foreach (User user in e.Results)     {         this.Users.Add(user);     } }), DispatcherPriority.Normal, null); 

I have this code in my viewmodel base class to get a Dispatcher:

if (Application.Current != null) {     this.Dispatcher = Application.Current.Dispatcher; } else {     this.Dispatcher = Dispatcher.CurrentDispatcher; } 

Is there something I need to do to initialise the Dispatcher for unit tests? The Dispatcher never runs the code in the delegate.

like image 237
Chris Shepherd Avatar asked Jul 09 '09 23:07

Chris Shepherd


2 Answers

By using the Visual Studio Unit Test Framework you don’t need to initialize the Dispatcher yourself. You are absolutely right, that the Dispatcher doesn’t automatically process its queue.

You can write a simple helper method “DispatcherUtil.DoEvents()” which tells the Dispatcher to process its queue.

C# Code:

public static class DispatcherUtil {     [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]     public static void DoEvents()     {         DispatcherFrame frame = new DispatcherFrame();         Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,             new DispatcherOperationCallback(ExitFrame), frame);         Dispatcher.PushFrame(frame);     }      private static object ExitFrame(object frame)     {         ((DispatcherFrame)frame).Continue = false;         return null;     } } 

You find this class too in the WPF Application Framework (WAF).

like image 91
jbe Avatar answered Sep 22 '22 18:09

jbe


We've solved this issue by simply mocking out the dispatcher behind an interface, and pulling in the interface from our IOC container. Here's the interface:

public interface IDispatcher {     void Dispatch( Delegate method, params object[] args ); } 

Here's the concrete implementation registered in the IOC container for the real app

[Export(typeof(IDispatcher))] public class ApplicationDispatcher : IDispatcher {     public void Dispatch( Delegate method, params object[] args )     { UnderlyingDispatcher.BeginInvoke(method, args); }      // -----      Dispatcher UnderlyingDispatcher     {         get         {             if( App.Current == null )                 throw new InvalidOperationException("You must call this method from within a running WPF application!");              if( App.Current.Dispatcher == null )                 throw new InvalidOperationException("You must call this method from within a running WPF application with an active dispatcher!");              return App.Current.Dispatcher;         }     } } 

And here's a mock one that we supply to the code during unit tests:

public class MockDispatcher : IDispatcher {     public void Dispatch(Delegate method, params object[] args)     { method.DynamicInvoke(args); } } 

We also have a variant of the MockDispatcher which executes delegates in a background thread, but it's not neccessary most of the time

like image 39
Orion Edwards Avatar answered Sep 22 '22 18:09

Orion Edwards