事件处理程序的单元测试

时间:2016-01-28 11:26:27

标签: c#

我想测试是否必须使用单元测试来引发我的事件。在我的项目中,我使用事件处理程序将进度值从一个类传递到另一个类。我在使用MVVM方法,因此,我将Model类中的值传递给了ViewModel类。如何为事件处理程序编写单元测试。

我尝试为名为GetTotalFileSize和TransferredFileSize的事件处理程序编写单元测试。但我无法在测试时引发事件。那么,我该怎么办?

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

        ripWatcherWindowShellViewModel.GetTotalFileSize  
        += delegate { eventRaised = true; };

        Assert.IsTrue(eventRaised);
    }

//生产代码是..

       public RipWatcherWindowShellViewModel(IWorkflowManager workflowManager)
       {
           WorkflowManager = workflowManager;                                      
           workflowManager.TransferredUsfFileSizeChanged 
           += workflowManager_TransferredUsfFileSizeChanged;

           workflowManager.GetTotalUsfFileSize 
           += workflowManager_GetTotalFileSize;
       }


       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 void workflowManager_TransferredUsfFileSizeChanged(object sender, FileSizeChangedEventArgs e)
    {
        if(e.FileSize !=0)
        {
            TransferredUsfFileSize = e.FileSize;
        }
        else
        {
            tempFileSize += TransferredUsfFileSize;
        }            
        /*Calculating progress percentage for updating progress bar*/
        ProgressPercentage = (int)(((TotalUsfFileSize -(TotalUsfFileSize-(tempFileSize+TransferredUsfFileSize)))/TotalUsfFileSize)* 100);
    }       


    public Double TransferredUsfFileSize
    {
        get;
        set;
    }

    /// <summary>Gets or sets the total file size</summary>
    public Double TotalUsfFileSize
    {
        get;
        set;
    }

    public IWorkflowManager WorkflowManager { get; set; }

    public int ProgressPercentage
    {
        get
        {
            return percentage;
        }
        set
        {
            percentage = value;
            OnPropertyChanged("ProgressPercentage");
        }
    }

价值来自......

     public void CopyEx(string sourceFilePath,string destinationFilePath)
    {
        try
        {
            lock (locker)
            {
                CopyFileEx(sourceFilePath, destinationFilePath,
                new CopyProgressRoutine(this.CopyProgressHandler), 
                IntPtr.Zero, ref pbCancel,
                CopyFileFlags.COPY_FILE_RESTARTABLE);
            }
        }
        catch(Exception ex)
        {
            throw new Exception(ex.message);
        }
    }

    private CopyProgressResult CopyProgressHandler(long total,
             long transferredFileSize, long streamSize,
             long StreamByteTrans, uint dwStreamNumber, 
             CopyProgressCallbackReason reason, 
             IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData)
    {
        TransferredUsfFileSizeChanged.SafeInvoke(this,
        new FileSizeChangedEventArgs(transferredFileSize));            

        return CopyProgressResult.PROGRESS_CONTINUE;
    }

    public EventHandler<FileSizeChangedEventArgs> 
    TransferredUsfFileSizeChanged;

0 个答案:

没有答案