MVVM Light&的单元测试DispatcherHelper [英] Unit testing with MVVM Light & DispatcherHelper

查看:226
本文介绍了MVVM Light&的单元测试DispatcherHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个利用MVVM Light Toolkit的SL4应用程序.在视图模型中,我称为数据服务,该数据服务从OData服务检索数据.在VM中,我正在使用DispatcherHelper实用程序类(MVVM Light的一部分)从传递到数据服务中的回调中的数据更新VM上的属性.例如,我的视图模型方法如下:

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) ? _currentUserLogin : _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);
}

当我运行我的SL应用程序时,一切都很好.但是,我遇到的问题是,当我尝试使用SL单元测试框架针对它编写单元测试时.我可以毫无问题地测试我的数据服务,但似乎DispatcherHelper在我的所有测试中都投入了一把扳手,因为DispatcherHelper.UIDispatcher在触发时始终为null.我假设它与初始化有关(位于我的SL应用程序的Application_Startup()中).我尝试在测试应用程序中对其进行初始化,但这无济于事.我也尝试过使用DispatcherHelper.CheckBeginInvokeOnUI(),但这对问题没有影响.

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.

想法?

推荐答案

AC,

我刚刚创建了一个简单的SL UT项目,并在App.XAML.CS中做到了

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();
}

然后我将其设置为测试(在tests.cs中):

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;
   });
}

对我有用.我什至在"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).

这篇关于MVVM Light&amp;的单元测试DispatcherHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆