title: Unit Testing CompositePresentationEvent when using the Dispatcher
body: <p>Im using the Prism/Composite Application Library and trying to unit test some code that subscribes to a CompositePresentationEvent using the EventAggregator. The code that is raising the event is raising it on another thread so I am subscribing to the event using ThreadOption.UIThread.</p>
  
  <p>When the event raises the callback it uses the application dispatcher to put it onto the UI thread. This is fine during normal execution but during a unit test there is no dispatcher. The code in CompositePresentationEvent looks like this:</p>
  
  <pre><code>    private IDispatcherFacade UIDispatcher
      {
          get
          {
              if (uiDispatcher == null)
              {
                  this.uiDispatcher = new DefaultDispatcher();
              }
  
              return uiDispatcher;
          }
      }
  
  
  
  public class DefaultDispatcher : IDispatcherFacade
  {
      /// &lt;summary&gt;
      /// Forwards the BeginInvoke to the current application's &lt;see cref="Dispatcher"/&gt;.
      /// &lt;/summary&gt;
      /// &lt;param name="method"&gt;Method to be invoked.&lt;/param&gt;
      /// &lt;param name="arg"&gt;Arguments to pass to the invoked method.&lt;/param&gt;
      public void BeginInvoke(Delegate method, object arg)
      {
          if (Application.Current != null)
          {
              Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, method, arg);
          }
      }
  }
  </code></pre>
  
  <p>The problem being that CompositePresentationEvent is tied to the DefaultDispatcher, and this dispatcher does nothing if there is no Application running.</p>
  
  <p>Has anyone had any success unit testing in this kind of situation? Any tips or workarounds to kick the dispatcher into life?</p>
  
  <p>I know I could make my callback internal and allow my unit test to call this method but I would prefer not to change my code and leave this approach as a last resort.</p>