title: Unit testing with MVVM Light & DispatcherHelper
body: <p>I have a SL4 app that utilizes the MVVM Light Toolkit. Within a view model, I call a data service that retrieves data from an OData service. Within the VM, I am using the DispatcherHelper utility class (part of MVVM Light) to update the property on the VM from the data in the callback I pass into the data service. For instance, my view model method looks like this:</p>
  
  <pre><code>public string CurrentUserLogin {
    get {
      if (string.IsNullOrEmpty(_currentUserLogin))
        RetrieveCurrentUserLogin();
      return string.IsNullOrEmpty(_currentUserLogin) ? _currentUserLogin : _currentUserLogin.Replace(@"\\", @"\");
    }
    set {
      if (_currentUserLogin != value) {
        _currentUserLogin = value;
        RaisePropertyChanged(CurrentUserLoginPropertyName);
      }
    }
  }
  
  private void RetrieveCurrentUserLogin() {
    DataService.GetCurrentUserLogin(result =&gt; {
      DispatcherHelper.UIDispatcher.BeginInvoke(() =&gt; {
        CurrentUserLogin = result;
      });
    });
  }
  </code></pre>
  
  <p>And here's what my data service looks like:</p>
  
  <pre><code>public void GetCurrentUserLogin(Action&lt;string&gt; callback) {
    // create query request
    var query = OnDemandContext.CreateQuery&lt;string&gt;("GetCurrentUserLogin");
    var request = (HttpWebRequest)WebRequest.Create(query.RequestUri);
    request.BeginGetResponse(asyncResult =&gt; {
      var responseStream = request.EndGetResponse(asyncResult).GetResponseStream();
      var responseDocument = XDocument.Load(responseStream);
      callback(responseDocument.Root.Value);
    }, null);
  }
  </code></pre>
  
  <p>Everything works great when I run my SL application. However the problem I have is when I try to write unit tests against it using the SL Unit Testing Framework. I can test my data service without an issue, but it seems the DispatcherHelper is throwing a wrench into all my tests as the DispatcherHelper.UIDispatcher is always null when fired. I'm assuming it has something to do with the initlization (which is in my SL app's Application_Startup()). I tried initializing it in my test app but that isn't helping. I've also tried using DispatcherHelper.CheckBeginInvokeOnUI() but that has no effect on the issue.</p>
  
  <p>Ideas?</p>