dimanche 31 janvier 2016

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.

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 event EventHandler<FileSizeChangedEventArgs> GetTotalUsfFileSize;

public void StartWorkflow()
{           
    totalFileSize= jobWatcher.StartWatching(HotFoldersCollection);

    //Event handler invoked 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.TransferredFileSize += delegate { eventRaised = true; };

    Assert.IsTrue(eventRaised);
}

Aucun commentaire:

Enregistrer un commentaire