我发现了几个 Stack Overflow 问题以及几篇已经涉及该主题的博客文章,但不幸的是它们都不能满足我的需求。我将从一些示例代码开始,以展示我想要完成的任务。
using System;
using System.Security.Permissions;
using System.Threading.Tasks;
using System.Windows.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyApp
{
[TestClass]
public class MyTests
{
private int _value;
[TestMethod]
public async Task TimerTest()
{
_value = 0;
var timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(10)};
timer.Tick += IncrementValue;
timer.Start();
await Task.Delay(15);
DispatcherUtils.DoEvents();
Assert.AreNotEqual(0, _value);
}
private void IncrementValue(object sender, EventArgs e)
{
_value++;
}
}
internal class DispatcherUtils
{
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void DoEvents()
{
var frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
Dispatcher.PushFrame(frame);
}
private static object ExitFrame(object frame)
{
((DispatcherFrame)frame).Continue = false;
return null;
}
}
}
如果我不使用 DispatcherTimer,而是使用普通计时器,则此代码可以正常工作。但 DispatcherTimer 永远不会触发。我缺少什么?我需要什么才能让它点火?
最佳答案
如果您可以在被测系统中避免使用 DispatcherTimer
并使用抽象(Rx 有一个很好的抽象,称为 IScheduler
),那就最好了。这种抽象允许您显式控制单元测试中的时间流,而不是使测试以 CPU 计时为条件。
但是,如果您现在只对单元测试感兴趣,那么您需要创建一个执行消息泵送操作的 STA 线程并且安装适当的Dispatcher
。所有“在调度程序上运行此代码”操作都只是将委托(delegate)包装在 Win32 消息中,如果您在 Dispatcher
中没有 Win32 消息泵送循环(< em>在创建计时器之前),那么这些消息将不会被处理。
最简单的方法是使用 here 中的 WpfContext
:
[TestMethod]
public async Task TimerTest()
{
await WpfContext.Run(() =>
{
_value = 0;
var timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(10)};
timer.Tick += IncrementValue;
timer.Start();
await Task.Delay(15);
Assert.AreNotEqual(0, _value);
});
}
同样,这种方法不合标准,因为它取决于时间。因此,如果您的防病毒软件感到不安并决定检查您的单元测试,它可能会虚假地失败。像 IScheduler
这样的抽象可以实现可靠的单元测试。
关于c# - 如何测试使用 DispatcherTimer 的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38083711/