如何单元测试与异步方法的视图模型。 [英] How to Unit Test a ViewModel with async method.

查看:145
本文介绍了如何单元测试与异步方法的视图模型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道从哪里开始,但让我给你我在哪里和一个简短的想法,我想达到的目标。我是很新的单元测试的MVVM,并具有在测试我使用PRISM委托指令属性公开命令困难。
我代表的命令调用,必须等待这样我就可以得到实际的结果异步方法。下面是一个ASYC方法是通过方法调用,我想测试一下。

I am not sure where to start but let me give you a brief idea on where I am and what I want to achieve. I am quite new to Unit Testing on MVVM and having difficulty on testing the commands that I exposed using PRISM delegate command properties. My delegate commands calls async method that has to be waited so I can get the actual result. Below is an asyc method that is called by method that I wanted to test.

 async void GetTasksAsync()
        {
            this.SimpleTasks.Clear();
            Func<IList<ISimpleTask>> taskAction = () =>
                {
                    var result = this.dataService.GetTasks();
                    if (token.IsCancellationRequested)
                        return null;
                    return result;
                };
            IsBusyTreeView = true;

            Task<IList<ISimpleTask>> getTasksTask = Task<IList<ISimpleTask>>.Factory.StartNew(taskAction, token);
            var l = await getTasksTask;          // waits for getTasksTask


            if (l != null)
            {
                foreach (ISimpleTask t in l)
                {
                    this.SimpleTasks.Add(t); // adds to ViewModel.SimpleTask
                }
            }
        }

也在这里是我的VM调用上述

also here is the command in my VM that calls the async method above

  this.GetTasksCommand = new DelegateCommand(this.GetTasks);
      void GetTasks()
        {
                GetTasksAsync();
        }

现在我的测试方法是这样

and now my Test Method goes like

 [TestMethod]
        public void Command_Test_GetTasksCommand()
        {
          MyViewModel.GetTaskCommand.Execute(); // this should populate ViewModel.SimpleTask 
          Assert.IsTrue(MyBiewModel.SimpleTask != null)
        } 

目前我所得到的是我的ViewModel.SimpleTask = NULL,这是因为它不等待异步方法来完成。据我所知,有一些相关主题,以这个已经在堆栈溢出,但我无法找到与我DelegateCommands东西。

Currently what I am getting is that my ViewModel.SimpleTask = null this is because it does not wait for the async method to finish. I understand there are some related topics to this already in stack overflow but I could not find something related to my DelegateCommands.

推荐答案

您的方法GetTasksAsync应该返回一个任务,所以你其实可以等待吧。

Your method GetTasksAsync should return a Task so you can actually wait for it.

我建议 Channel9上一个专门的 href=\"http://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Tip-1-Async-void-is-for-top-level-event-handlers-only\"相对=nofollow>这个情节解释你的问题。

I recommend this series on Channel9 an specially this episode explaining your problem.

只是为了是清楚的:只要改变GetTasksAsync的签名

Just to make is clear: Simply changing the signature of GetTasksAsync to

Task GetTasksAsync();

可以让你做到这一点:

allows you to do this:

var t = GetAsync();
t.Wait();
Assert(...);

如果你真的想在你的单元测试来测试命令,而不是实际的方法由命令调用,您可以使用一个字段在你的视图模型以存储任务等待(不那么干净),或者被什么东西取代您的DelegateCommand像在这个帖子中描述: Awaitable DelegateCommand

更新:除了博客文章,并考虑你正在使用PRISM,你应该看看一个项目来自同一团队PRISM科纳。他们实际上实施<一个href=\"http://konaguidance.$c$cplex.com/SourceControl/changeset/view/cb2b257e240e#StoreApps.Infrastructure/DelegateCommand.cs\"相对=nofollow> DelegateCommand支持AsyncHandlers

Update: In addition to the blog post and considering you are using PRISM, you should have a look a Project Kona from the same team as PRISM. They actually implemented DelegateCommand to support AsyncHandlers

这篇关于如何单元测试与异步方法的视图模型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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