【问题标题】:How do I unit test an async ICommand in MVVM?如何在 MVVM 中对异步 ICommand 进行单元测试?
【发布时间】:2015-04-28 12:35:31
【问题描述】:

我一直在谷歌搜索甚至是 Bing-ing,但我没有想出任何令人满意的东西。

我有一个 ViewModel,它有一些命令,例如:SaveCommandNewCommandDeleteCommand。我的SaveCommand 执行保存到文件的操作,我希望这是一个async 操作,以便 UI 不会等待它。

我的SaveCommandAsyncCommand 的一个实例,它实现了ICommand

 SaveCommand = new AsyncCommand(
  async param =>
        {
            Connection con = await Connection.GetInstanceAsync(m_configurationPath);
            con.Shoppe.Configurations = new List<CouchDbConfig>(m_configurations);
            await con.SaveConfigurationAsync(m_configurationPath);
            //now that its saved, we reload the Data.
            await LoadDataAsync(m_configurationPath);
        }, 
 ...etc

现在我正在为我的 ViewModel 构建一个测试。在其中,我使用NewCommand 创建了一个新事物,我对其进行了修改,然后使用SaveCommand

vm.SaveCommand.Execute(null);
Assert.IsFalse(vm.SaveCommand.CanExecute(null));

我的SaveCommandCanExecute 方法(未显示)应该在保存项目后返回False(保存未更改的项目没有意义)。但是,上面显示的 Assert 一直失败,因为我没有等待 SaveCommand 完成执行。

现在,我不能等待它完成执行,因为我不能。 ICommand.Execute 不返回 Task。如果我将AsyncCommand 更改为使其Execute 返回Task,那么它将无法正确实现ICommand 接口。

所以,出于测试目的,我认为我现在唯一能做的就是让AsynCommand 拥有一个新功能:

public async Task ExecuteAsync(object param) { ... }

因此,我的测试将运行(和awaitExecuteAsync 函数,XAML UI 将运行ICommand.Execute 方法,其中它不运行await

我不喜欢按照我的想法、希望和希望有更好的方法来执行我提出的解决方法。

我的建议合理吗?有没有更好的办法?

【问题讨论】:

  • 您的命令中是否有任何属性显示“正在运行”或“正在执行”?也许一种方法是在命令中添加一个执行标志。也可以解决测试失败的原因,您可以使用 CanExecute 中的 Executing 标志来确保它们不会两次运行该命令。
  • 我明白你的意思。让我试试看。 *很快
  • 其实,AsyncCommand 已经有了 Executing 标志。当我最初发现问题时,TBH,我没有使用 AsyncCommand,而是另一个没有使用的东西。也许我现在真的没有问题! .. 测试
  • @RonBeyer:单元测试中的断言现在通过了,我的 UI 也可以工作了,这要归功于 Executing 标志。但是,当单元测试结束时,我从 SaveCommand 中得到一个 ThreadAbortException 仍然将项目保存到文件中。虽然,“不是一个真正的问题”,但我想知道我是否可以彻底结束这一切。
  • 我只是放了一会儿(vm.SaveCommand.Executing);在断言下验证操作是否完成。它会使测试运行时间更长,但可能会干净地执行。

标签: c# unit-testing mvvm task-parallel-library icommand


【解决方案1】:

您的建议是合理的,这正是AsyncCommand implementation created by Stephen Cleary 所做的(他是最重要的experts on the subject of async 代码恕我直言之一)

这是文章中代码的完整实现(加上我为我使用的用例所做的一些调整。)

AsyncCommand.cs

/*
 * Based on the article: Patterns for Asynchronous MVVM Applications: Commands
 * http://msdn.microsoft.com/en-us/magazine/dn630647.aspx
 * 
 * Modified by Scott Chamberlain 11-19-2014
 * - Added parameter support 
 * - Added the ability to shut off the single invocation restriction.
 * - Made a non-generic version of the class that called the generic version with a <object> return type.
 */
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Infrastructure
{
    public class AsyncCommand : AsyncCommand<object>
    {
        public AsyncCommand(Func<object, Task> command) 
            : base(async (parmater, token) => { await command(parmater); return null; }, null)
        {
        }

        public AsyncCommand(Func<object, Task> command, Func<object, bool> canExecute)
            : base(async (parmater, token) => { await command(parmater); return null; }, canExecute)
        {
        }

        public AsyncCommand(Func<object, CancellationToken, Task> command)
            : base(async (parmater, token) => { await command(parmater, token); return null; }, null)
        {
        }

        public AsyncCommand(Func<object, CancellationToken, Task> command, Func<object, bool> canExecute)
            : base(async (parmater, token) => { await command(parmater, token); return null; }, canExecute)
        {
        }
    }

    public class AsyncCommand<TResult> : AsyncCommandBase, INotifyPropertyChanged
    {
        private readonly Func<object, CancellationToken, Task<TResult>> _command;
        private readonly CancelAsyncCommand _cancelCommand;
        private readonly Func<object, bool> _canExecute;
        private NotifyTaskCompletion<TResult> _execution;
        private bool _allowMultipleInvocations;

        public AsyncCommand(Func<object, Task<TResult>> command)
            : this((parmater, token) => command(parmater), null)
        {
        }

        public AsyncCommand(Func<object, Task<TResult>> command, Func<object, bool> canExecute)
            : this((parmater, token) => command(parmater), canExecute)
        {
        }

        public AsyncCommand(Func<object, CancellationToken, Task<TResult>> command)
            : this(command, null)
        {
        }

        public AsyncCommand(Func<object, CancellationToken, Task<TResult>> command, Func<object, bool> canExecute)
        {
            _command = command;
            _canExecute = canExecute;
            _cancelCommand = new CancelAsyncCommand();
        }


        public override bool CanExecute(object parameter)
        {
            var canExecute = _canExecute == null || _canExecute(parameter);
            var executionComplete = (Execution == null || Execution.IsCompleted);

            return canExecute && (AllowMultipleInvocations || executionComplete);
        }

        public override async Task ExecuteAsync(object parameter)
        {
            _cancelCommand.NotifyCommandStarting();
            Execution = new NotifyTaskCompletion<TResult>(_command(parameter, _cancelCommand.Token));
            RaiseCanExecuteChanged();
            await Execution.TaskCompletion;
            _cancelCommand.NotifyCommandFinished();
            RaiseCanExecuteChanged();
        }

        public bool AllowMultipleInvocations
        {
            get { return _allowMultipleInvocations; }
            set
            {
                if (_allowMultipleInvocations == value)
                    return;

                _allowMultipleInvocations = value;
                OnPropertyChanged();
            }
        }

        public ICommand CancelCommand
        {
            get { return _cancelCommand; }
        }

        public NotifyTaskCompletion<TResult> Execution
        {
            get { return _execution; }
            private set
            {
                _execution = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        private sealed class CancelAsyncCommand : ICommand
        {
            private CancellationTokenSource _cts = new CancellationTokenSource();
            private bool _commandExecuting;

            public CancellationToken Token { get { return _cts.Token; } }

            public void NotifyCommandStarting()
            {
                _commandExecuting = true;
                if (!_cts.IsCancellationRequested)
                    return;
                _cts = new CancellationTokenSource();
                RaiseCanExecuteChanged();
            }

            public void NotifyCommandFinished()
            {
                _commandExecuting = false;
                RaiseCanExecuteChanged();
            }

            bool ICommand.CanExecute(object parameter)
            {
                return _commandExecuting && !_cts.IsCancellationRequested;
            }

            void ICommand.Execute(object parameter)
            {
                _cts.Cancel();
                RaiseCanExecuteChanged();
            }

            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }

            private void RaiseCanExecuteChanged()
            {
                CommandManager.InvalidateRequerySuggested();
            }
        }
    }
}

AsyncCommandBase.cs

/*
 * Based on the article: Patterns for Asynchronous MVVM Applications: Commands
 * http://msdn.microsoft.com/en-us/magazine/dn630647.aspx
 */
using System;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Infrastructure
{
    public abstract class AsyncCommandBase : IAsyncCommand
    {
        public abstract bool CanExecute(object parameter);

        public abstract Task ExecuteAsync(object parameter);

        public async void Execute(object parameter)
        {
            await ExecuteAsync(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        protected void RaiseCanExecuteChanged()
        {
            CommandManager.InvalidateRequerySuggested();
        }
    }
}

NotifyTaskCompletion.cs

/*
 * Based on the article: Patterns for Asynchronous MVVM Applications: Commands
 * http://msdn.microsoft.com/en-us/magazine/dn630647.aspx
 * 
 * Modifed by Scott Chamberlain on 12/03/2014
 * Split in to two classes, one that does not return a result and a 
 * derived class that does.
 */

using System;
using System.ComponentModel;
using System.Threading.Tasks;

namespace Infrastructure
{
    public sealed class NotifyTaskCompletion<TResult> : NotifyTaskCompletion
    {
        public NotifyTaskCompletion(Task<TResult> task)
            : base(task)
        {
        }

        public TResult Result
        {
            get
            {
                return (Task.Status == TaskStatus.RanToCompletion) ?
                    ((Task<TResult>)Task).Result : default(TResult);
            }
        }
    }

    public class NotifyTaskCompletion : INotifyPropertyChanged
    {
        public NotifyTaskCompletion(Task task)
        {
            Task = task;
            if (!task.IsCompleted)
                TaskCompletion = WatchTaskAsync(task);
            else
                TaskCompletion = Task;
        }

        private async Task WatchTaskAsync(Task task)
        {
            try
            {
                await task;
            }
            catch
            {
                //This catch is intentionally empty, the errors will be handled lower on the "task.IsFaulted" branch.
            }
            var propertyChanged = PropertyChanged;
            if (propertyChanged == null)
                return;
            propertyChanged(this, new PropertyChangedEventArgs("Status"));
            propertyChanged(this, new PropertyChangedEventArgs("IsCompleted"));
            propertyChanged(this, new PropertyChangedEventArgs("IsNotCompleted"));
            if (task.IsCanceled)
            {
                propertyChanged(this, new PropertyChangedEventArgs("IsCanceled"));
            }
            else if (task.IsFaulted)
            {
                propertyChanged(this, new PropertyChangedEventArgs("IsFaulted"));
                propertyChanged(this, new PropertyChangedEventArgs("Exception"));
                propertyChanged(this, new PropertyChangedEventArgs("InnerException"));
                propertyChanged(this, new PropertyChangedEventArgs("ErrorMessage"));
            }
            else
            {
                propertyChanged(this, new PropertyChangedEventArgs("IsSuccessfullyCompleted"));
                propertyChanged(this, new PropertyChangedEventArgs("Result"));
            }
        }

        public Task Task { get; private set; }
        public Task TaskCompletion { get; private set; }
        public TaskStatus Status { get { return Task.Status; } }
        public bool IsCompleted { get { return Task.IsCompleted; } }
        public bool IsNotCompleted { get { return !Task.IsCompleted; } }
        public bool IsSuccessfullyCompleted
        {
            get
            {
                return Task.Status ==
                    TaskStatus.RanToCompletion;
            }
        }
        public bool IsCanceled { get { return Task.IsCanceled; } }
        public bool IsFaulted { get { return Task.IsFaulted; } }
        public AggregateException Exception { get { return Task.Exception; } }
        public Exception InnerException
        {
            get
            {
                return (Exception == null) ?
                    null : Exception.InnerException;
            }
        }
        public string ErrorMessage
        {
            get
            {
                return (InnerException == null) ?
                    null : InnerException.Message;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

【讨论】:

  • 哇塞。这似乎可以做所有事情,包括厨房水槽!我同意这是一个看似非常完整的实现,包括取消等。现在,我的问题是,我将哪个答案标记为正确?对于我的即时需求,可能我暂时需要的只是测试用例中的 while 循环。但我知道这个答案是最完整的,并最终解决了我眼前的问题,可能是我将来会遇到的所有问题。
【解决方案2】:

看起来答案是使用带有AsyncCommand 对象的标志。在CanExecute 方法中使用AsyncCommandExecuting 标志将确保用户在另一个实例正在运行时无法执行该命令。

同样对于您的单元测试,您可以使用 while 循环使其在断言之后等待:

while (vm.SaveCommand.Executing) ;

这样测试就干净利落地退出了。

【讨论】:

  • 所以你的意思是这比添加另一个返回Task的方法更干净?
  • 是的,如果这意味着Task 仅用于测试目的。我会避免编写额外的代码来通过测试,因为它有点违背测试的目的。
  • 我认为它更干净。因为没有两个令人困惑的 Execute 方法。
  • @RonBeyer 你的方法再好不过了。您还引入了一个仅用于测试的属性。这对你有好处吗?检查this article stephen 建议添加另一个方法,该方法返回一个很好地封装在另一个接口中的任务。
  • @SriramSakthivel 我没有创建属性,它已经存在于 AsyncCommand 对象上,请查看问题的 cmets。
【解决方案3】:

查看其他答案

执行while (vm.SaveCommand.Executing) ; 似乎忙于等待,我更愿意避免这种情况

另一个使用来自 Stephen Cleary 的 AsyncCommand 的解决方案似乎有点对于这样一个简单的任务来说太过分了

我提出的方法不会破坏封装——Save 方法不会暴露任何内部结构。它只是提供了访问相同功能的另一种方式。

我的解决方案似乎以简单直接的方式涵盖了所需的一切。

建议

我建议重构这段代码:

SaveCommand = new AsyncCommand(
    async param =>
    {
        Connection con = await Connection.GetInstanceAsync(m_configurationPath);
        con.Shoppe.Configurations = new List<CouchDbConfig>(m_configurations);
        await con.SaveConfigurationAsync(m_configurationPath);
        //now that its saved, we reload the Data.
        await LoadDataAsync(m_configurationPath);
    });

到:

SaveCommand = new RelayCommand(async param => await Save(param));

public async Task Save(object param)
{
    Connection con = await Connection.GetInstanceAsync(m_configurationPath);
    con.Shoppe.Configurations = new List<CouchDbConfig>(m_configurations);
    await con.SaveConfigurationAsync(m_configurationPath);
    //now that its saved, we reload the Data.
    await LoadDataAsync(m_configurationPath);
}

请注意:我将 AsyncCommand 更改为 RelayCommand,它可以在任何 MVVM 框架中找到。它只是接收一个动作作为参数,并在调用ICommand.Execute 方法时运行它。

单元测试

我使用支持async 测试的 NUnit 框架做了一个示例:

[Test]
public async Task MyViewModelWithAsyncCommandsTest()
{
    // Arrange
    // do view model initialization here

    // Act
    await vm.Save(param);

    // Assert
    // verify that what what you expected actually happened
}

并在视图中像平常一样绑定命令:

Command="{Binding SaveCommand}"

【讨论】:

  • 我认为 Stephen Cleary Way™ 将在您的视图模型中使用IAsyncCommand,然后在您的测试中使用await vm.SaveCommand.ExecuteAsync()。 (不是所有的AsyncCommand 实现都有这个方法(例如,DevExpress 没有),但是Cleary's does。)
  • 只是一个小补充:我会将 Save 方法标记为“内部”,并在 YourAssembly 的 AssemblyInfo.cs 中添加 [assembly: InternalsVisibleTo("YourAssembly.Tests")] (其中存在 Save 方法)。我不喜欢仅仅为了单元测试而将事情“公开”。
猜你喜欢
  • 2013-01-27
  • 2013-03-21
  • 1970-01-01
  • 2014-02-14
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
相关资源
最近更新 更多