开发者

Unit testing with MVVM Light & DispatcherHelper

开发者 https://www.devze.com 2023-02-05 17:04 出处:网络
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 clas

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:

public string CurrentUserLogin {
  get {
    if (string.IsNullOrEmpty(_currentUserLogin))
      RetrieveCurrentUserLogin();
    return string.IsNullOrEmpty(_currentUserLogin) ? _current开发者_高级运维UserLogin : _currentUserLogin.Replace(@"\\", @"\");
  }
  set {
    if (_currentUserLogin != value) {
      _currentUserLogin = value;
      RaisePropertyChanged(CurrentUserLoginPropertyName);
    }
  }
}

private void RetrieveCurrentUserLogin() {
  DataService.GetCurrentUserLogin(result => {
    DispatcherHelper.UIDispatcher.BeginInvoke(() => {
      CurrentUserLogin = result;
    });
  });
}

And here's what my data service looks like:

public void GetCurrentUserLogin(Action<string> callback) {
  // create query request
  var query = OnDemandContext.CreateQuery<string>("GetCurrentUserLogin");
  var request = (HttpWebRequest)WebRequest.Create(query.RequestUri);
  request.BeginGetResponse(asyncResult => {
    var responseStream = request.EndGetResponse(asyncResult).GetResponseStream();
    var responseDocument = XDocument.Load(responseStream);
    callback(responseDocument.Root.Value);
  }, null);
}

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.

Ideas?


AC,

I just created a simple SL UT project and I did this in the App.XAML.CS

private void Application_Startup(object sender, StartupEventArgs e)
{
  RootVisual = UnitTestSystem.CreateTestPage();
  DispatcherHelper.Initialize();
}

Then I set this as the test (in the tests.cs):

[TestMethod]
public void TestMethod1()
{
   Assert.IsNotNull(DispatcherHelper.UIDispatcher, "UI Dispatcher should not be null");

   DispatcherHelper.CheckBeginInvokeOnUI(() =>
   {
       // Do nothing
       var x = 1;
   });
}

That worked for me. I even set a break point on the "var x = 1;" and it hit the breakpoint. Does this solve your problem? (if so please mark it as the answer).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号