我需要一个用于测试事件处理程序的测试用例。在我的项目中,我只是使用事件处理程序将总文件大小值从Model
类传递到ViewModel
类。
为事件处理程序编写测试用例以及在测试用例中我必须做什么的所有方法是什么?
我的测试用例无效..
public RipWatcherWindowShellViewModel(IWorkflowManager workflowManager)
{
WorkflowManager = workflowManager;
workflowManager.GetTotalUsfFileSize += workflowManager_GetTotalFileSize;
}
/// <summary>Delegate for workflowManager get total file size</summary>
/// <param name="sender">The sender object</param>
/// <param name="e">The FileSizeChangedEventArgs object</param>
public void workflowManager_GetTotalFileSize(object sender, FileSizeChangedEventArgs e)
{
if(e.FileSize== 0)
{
throw new ArgumentException("We cannot calculate progress percentage because total file size is 0");
}
TotalUsfFileSize = e.FileSize;
}
public class WorkflowManager
{
public event EventHandler<FileSizeChangedEventArgs> GetTotalUsfFileSize;
public void StartWorkflow()
{
totalFileSize= jobWatcher.StartWatching(HotFoldersCollection);
//Event handler Raised here....
GetTotalUsfFileSize.SafeInvoke(this, new FileSizeChangedEventArgs(totalFileSize));
}
}
public class FileSizeChangedEventArgs:EventArgs
{
public FileSizeChangedEventArgs (Double fileSize)
{
FileSize = fileSize;
}
public Double FileSize
{
get;
private set;
}
}
[Test]
public void IsGetTotalFileSizeEventFired()
{
worflowManager = new Mock<IWorkflowManager>().Object;
ripWatcherWindowShellViewModel = new RipWatcherWindowShellViewModel(worflowManager);
ripWatcherWindowShellViewModel.GetTotalUsfFileSize += delegate { eventRaised = true; };
Assert.IsTrue(eventRaised);
}
答案 0 :(得分:0)
如果您正在测试您的事件处理程序是否有效,只需使用您要测试的事件参数调用事件处理程序。
在您的情况下,我假设您要测试您的活动是否被提出。这将取决于StartWorkFlow
内的任何调用是否具有任何外部依赖性(如实际文件系统。如果确实如此,那么您将跨越到集成测试区域而不再是单元测试区域)。在这种情况下,请确保jobWatcher
也被模拟,或使StartWorkFlow
为虚拟,以便您可以改变其行为以适合您的测试。