c# - Unit Test for Event handlers -
i want test whether event has raised or not using unit testing. in project, passed progress value 1 class using event handler. working on mvvm method so, passed value model class viewmodel class. how write unit test event handler.
i try write unit test event handlers called gettotalfilesize , transferredfilesize.but not raise events on test. so, have do?
[test] public void isgettotalfilesizeeventfired() { worflowmanager = new mock<iworkflowmanager>().object; ripwatcherwindowshellviewmodel = new ripwatcherwindowshellviewmodel(worflowmanager); ripwatcherwindowshellviewmodel.gettotalfilesize += delegate { eventraised = true; }; assert.istrue(eventraised); }
//production code is..
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 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 updating progress bar*/ progresspercentage = (int)(((totalusffilesize -(totalusffilesize-(tempfilesize+transferredusffilesize)))/totalusffilesize)* 100); } public double transferredusffilesize { get; set; } /// <summary>gets or sets total file size</summary> public double totalusffilesize { get; set; } public iworkflowmanager workflowmanager { get; set; } public int progresspercentage { { return percentage; } set { percentage = value; onpropertychanged("progresspercentage"); } }
the value came from..
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;
Comments
Post a Comment