c# Unit test a delegate event handler -
c# Unit test a delegate event handler -
i have countdowntimer class updates controller updates user interface. problem have when run unit tests nllreferenceexception because event handlder(tick) never initialized null. best possible solution problem? or should go differently. thanks
public class countdowntimer : icountdowntimer { private int seconds; // time in seconds private int resetvalue; // time in seconds private system.windows.forms.timer timer1; public event tickhandler tick; public eventargs e = null; public delegate void tickhandler(countdowntimer m, eventargs e, int seconds); public countdowntimer(int seconds) { this.seconds = seconds; resetvalue = seconds; timer1 = new system.windows.forms.timer(); timer1.tick += new eventhandler(timer1_tick); // add together handler(timer1_tick) timer1.interval = 1000; // 1 sec } private void timer1_tick(object sender, eventargs e) { calltickhandler(); if (getseconds() == 0) // stop timer @ 0 { timer1.stop(); // stop timer } else { if (getseconds() % 60 == 0 || getseconds() >= 1 && getseconds() <= 10) { calltickhandler(); } } seconds--; // decrement seconds } public void starttimer() { timer1.start(); } public void stoptimer() { timer1.stop(); } public void resettimer() { timer1.stop(); seconds = resetvalue; calltickhandler(); } public void settimer(int seconds) { timer1.stop(); this.seconds = getseconds(); resetvalue = getseconds(); calltickhandler(); } internal void calltickhandler() { tick(this, e, getseconds()); } public boolean isenabled() { homecoming timer1.enabled; } public int getseconds() { homecoming seconds; } } public class controller : applicationcontext { //store reference ui internal frmmain mainui { get; set; } private int seconds = 300; countdowntimer timer; public controller() { mainui = new frmmain(this); //we can necessary checks or changes mainui here before becomes visible mainui.show(); timer = new countdowntimer(seconds); subscribetoticklistener(timer); tickupdate(seconds); } internal void tickupdate(string seconds) { mainui.lbltimer.text = ("" + convert.toint32(seconds) / 60).padleft(2, '0') + "m:" + ("" + convert.toint32(seconds) % 60).padleft(2, '0') + "s"; } internal void tickupdate(int seconds) { mainui.lbltimer.text = ("" + seconds / 60).padleft(2, '0') + "m:" + ("" + seconds % 60).padleft(2, '0') + "s"; if (seconds <= 10) { //ss.speak(seconds.tostring()); } else { //ss.speak((seconds / 60).tostring() + " min warning"); } } internal void starttimer() { timer.starttimer(); } internal void resettimer() { timer.resettimer(); } internal void stoptimer() { timer.stoptimer(); } internal void settimer(int seconds) { timer.settimer(seconds); } public void subscribetoticklistener(countdowntimer cdt) { cdt.tick += new countdowntimer.tickhandler(tickmsgrecieved); } public void tickmsgrecieved(countdowntimer cdt, eventargs e, int seconds) { tickupdate(seconds); tickupdate(seconds.tostring()); } } public class countdowntimertests { private countdowntimer t = new countdowntimer(300); [testmethod()] public void countdowntimertest() { countdowntimer t = new countdowntimer(300); } [testmethod()] public void starttimertest() { //countdowntimer t = new countdowntimer(300); t.starttimer(); boolean expected = t.isenabled(); boolean actual = true; assert.areequal(expected, actual); } [testmethod()] public void stoptimertest() { //countdowntimer t = new countdowntimer(300); t.starttimer(); t.stoptimer(); boolean expected = t.isenabled(); boolean actual = false; assert.areequal(expected, actual); } [testmethod()] public void resettimertest() { int expected = t.getseconds(); t.resettimer(); int actual = t.getseconds(); assert.areequal(expected, actual); } [testmethod()] public void settimertest() { int expected = t.getseconds(); t.settimer(120); int actual = t.getseconds(); assert.areequal(expected, actual); } }
in case can utilize mock implementation of event. add together next mock event test class simulate consumer of countdowntimer class.
[testinitialize] public void testsetup() { t.tick += new countdowntimer.tickhandler(mocktickevent); } [testcleanup] public void testcleanup() { t.tick -= mocktickevent; } void mocktickevent(countdowntimer m, eventargs e, int seconds) { ///you may need add together farther test code here cover code return; }
c# unit-testing delegates
Comments
Post a Comment