【问题标题】:Unit testing an async method which invokes DispatcherHelper.CheckBeginInvokeOnUI对调用 DispatcherHelper.CheckBeginInvokeOnUI 的异步方法进行单元测试
【发布时间】:2016-10-04 09:07:38
【问题描述】:

我在 Visual Studio 2012 中使用 MVVM Light 5.2。我的单元测试是 MS 测试,我不知道如何测试我的异步方法,因为 DispatcherHelper 不会调用我的操作。 使用以下测试,在调试中永远不会到达 Thread.Sleep。

在 MVVM 光源中 DispatcherHelper.CheckBeginInvokeOnUi 调用 UIDispatcher.BeginInvoke(action),什么都没有发生。 我究竟做错了什么 ?

    [TestMethod]
    public void TestMethod1()
    {
        DispatcherHelper.Initialize();
        TestedMethod();
        // Do assert here
    }

    void TestedMethod()
    {
        ThreadPool.QueueUserWorkItem((o) =>
        {
            // Do stuff
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                // Do stuff
                Thread.Sleep(1); // Breakpoint here
            });
        });
    }

【问题讨论】:

    标签: c# wpf unit-testing mvvm-light


    【解决方案1】:

    如果它可以提供帮助,因为我不想修改 MVVM 光源,我最后在 DispatcherHelper 上编写了一个代理,它在测试方法中初始化时直接调用操作。它还处理设计模式。

    我只需要通过 UIDispatcher 搜索/替换每个 DispatcherHelper,仅此而已。

    这里是代码:

    public static class UIDispatcher
    {
        private static bool _isTestInstance;
    
        /// <summary>
        ///      Executes an action on the UI thread. If this method is called from the UI
        ///     thread, the action is executed immendiately. If the method is called from
        ///     another thread, the action will be enqueued on the UI thread's dispatcher
        ///     and executed asynchronously.
        ///     For additional operations on the UI thread, you can get a reference to the
        ///     UI thread's dispatcher thanks to the property GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher
        /// </summary>
        /// <param name="action">The action that will be executed on the UI thread.</param>
        public static void CheckBeginInvokeOnUI(Action action)
        {
            if (action == null)
            {
                return;
            }
            if ((_isTestInstance) || (ViewModelBase.IsInDesignModeStatic))
            {
                action();
            }
            else
            {
                DispatcherHelper.CheckBeginInvokeOnUI(action);
            }
        }
    
        /// <summary>
        ///     This method should be called once on the UI thread to ensure that the GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher
        ///     property is initialized.
        ///     In a Silverlight application, call this method in the Application_Startup
        ///     event handler, after the MainPage is constructed.
        ///     In WPF, call this method on the static App() constructor.
        /// </summary>
        /// <param name="isTestInstance"></param>
        public static void Initialize(bool isTestInstance = false)
        {
            _isTestInstance = isTestInstance;
            if (!_isTestInstance)
                DispatcherHelper.Initialize();
        }
    
        /// <summary>
        ///    Resets the class by deleting the GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher
        /// </summary>
        public static void Reset()
        {
            if (!_isTestInstance)
                DispatcherHelper.Reset();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2013-03-15
      • 2017-12-21
      • 2010-11-13
      • 2023-03-06
      • 1970-01-01
      • 2016-03-19
      相关资源
      最近更新 更多