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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With