简体   繁体   English

为事件处理程序编写单元测试C#

[英]Writing Unit test for event handlers C#

I need a test case for testing event handlers. 我需要一个测试用例来测试事件处理程序。 In my project, I just pass the total file size value from the Model class to ViewModel class using event handlers. 在我的项目中,我只是使用事件处理程序将总文件大小值从Model类传递给ViewModel类。

What are all the ways to write test case for event handlers and what I have to do in my test case? 有什么方法可以为事件处理程序编写测试用例?在测试用例中我必须做什么?

My test case is not working.. 我的测试用例无法正常工作。

View Model 查看模型

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;
}

Model 模型

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));
     }
  }

Event Handler 事件处理程序

public class FileSizeChangedEventArgs:EventArgs
{

    public FileSizeChangedEventArgs (Double fileSize)
    {
        FileSize = fileSize;
    }

    public Double FileSize
    {
        get;
        private set;
    }
}

My Test case 我的测试用例

[Test]
public void IsGetTotalFileSizeEventFired()
{
    worflowManager = new Mock<IWorkflowManager>().Object;

    ripWatcherWindowShellViewModel = new RipWatcherWindowShellViewModel(worflowManager);
    ripWatcherWindowShellViewModel.GetTotalUsfFileSize += delegate { eventRaised = true; };

    Assert.IsTrue(eventRaised);
}

If you are testing that your event handler works, simply call the event handler with the event args that you want to test. 如果要测试事件处理程序是否正常运行,只需使用要测试的事件args调用事件处理程序即可。

In your case I assume you want to test that your event is raised. 在您的情况下,我假设您要测试是否引发了事件。 This will be dependent on if any calls inside your StartWorkFlow have any external dependencies (like to the actual file system. If it does, then you are crossing over into integration test territory and no longer unit test territory). 这将取决于StartWorkFlow内部的任何调用是否具有任何外部依赖项(例如与实际文件系统类似)。如果有,则您将进入集成测试领域,而不再是单元测试领域。 In that instance, make sure jobWatcher is also mocked, or make StartWorkFlow virtual so that you can alter its behavior to suit your test. 在这种情况下,请确保也jobWatcher ,或者将StartWorkFlow虚拟化,以便您可以更改其行为以适合您的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM