title: How to Unit Test WPF + TPL
body: <p>I'm running a simple WPF application which uses a Event-Based/TPL approach to process data.
  Three classes are used in this Example(View, Presenter, Model)</p>
  
  <pre><code>Snip of Presenter:
  
  internal void btn_test_Click(object sender, EventArgs e)
  {
      Task&lt;Person&gt;.Factory.StartNew(() =&gt; GetPerson(id)).ContinueWith(UpdateTest, TaskScheduler.FromCurrentSynchronizationContext());
  }
  
  public Person GetPerson(int id)
  {
      Person p = Model.GetPerson(id);
      return p;
  }
  
  private void UpdateTest(Task&lt;Person&gt; task)
  {
      Person p = task.Result;
      window.tb_test.Text = p.ID + " " + p.Name; // PROBLEM HERE
  }
  </code></pre>
  
  <p>So, I'm getting an event from the View, start a new Task to get Data from my DB or Service and  update the UI afterwards. Work perfectly fine.</p>
  
  <p>Now I want to create a Unit test for this scenario. Is the displayed value correct?</p>
  
  <pre><code>[TestMethod]
  
  SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
  
  waitHandle = new ManualResetEvent(false);
  
  WPF.MainWindowView mwv = new MainWindowView();
  mwv.btn_test.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
  mwv.tb_test.TextChanged += (s, e) =&gt; waitHandle.Set();
  
  waitHandle.WaitOne();
  
  Assert.AreEqual("43 displayvalue", mwv.tb_test.Text);
  </code></pre>
  
  <p>WPF Application works fine, but there's an InvalidoperationException at tests. I tried using a Dispatcher for updating UI components by calling</p>
  
  <pre><code>window.tb_test.Dispatcher.BeginInvoke((ThreadStart)delegate {window.tb_test.Text = t.ID + " " + t.Name});
  </code></pre>
  
  <p>in UpdateTest, but the "tb_test.textChanged" event doesn't get called at my testing module altough the app itself works perfectly fine.</p>