27.07.2013 Views

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

2 Why We Need Model-Based Testing

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Overview 41<br />

// Typical trouble-free scenario<br />

[Test]<br />

[Category("Typical")]<br />

public void Typical()<br />

{<br />

Controller c = new Controller();<br />

c.ReceiveEvent(ControlEvent.Timeout, null);<br />

c.DispatchHandler(); // Reset expected<br />

c.ReceiveEvent(ControlEvent.Message, "99.9");<br />

c.DispatchHandler(); // CheckMessage<br />

c.ReceiveEvent(ControlEvent.Timeout, null);<br />

c.DispatchHandler(); // Poll<br />

c.ReceiveEvent(ControlEvent.Message, "100.0");<br />

c.DispatchHandler(); // CheckMessage<br />

// Command before Timeout scheduled by second CheckMessage<br />

c.ReceiveEvent(ControlEvent.Command, null);<br />

c.DispatchHandler(); // Calibrate<br />

// Exit before Timeout scheduled by second CheckMessage<br />

c.ReceiveEvent(ControlEvent.Exit, null);<br />

}<br />

Console.Write("\nDid the expected handlers run? [y/n] ");<br />

string answer = Console.ReadLine();<br />

Assert.IsTrue(answer[0] == ’y’ || answer[0] == ’Y’);<br />

Figure 3.7. Test method for a typical controller run.<br />

respond to the reset command – the call to StartTimer ensures that a time-out event<br />

will occur. This will enable the ReportLostMessage handler.<br />

In a similar way, the other guards and handlers are all coded to work together,<br />

to generate the behavior described in Section 3.1. To get the controller started,<br />

the constructor of the Simulator application starts the timer, and the variables in<br />

Controller are all initialized to enable the Reset handler when the time-out occurs.<br />

3.3 Unit testing<br />

The controller logic is intricate, so we suspect that it might be defective. <strong>We</strong> resolve<br />

to do some testing to check our intuitions that the controller behaves as we intend.<br />

<strong>We</strong> can test a reactive program in a sandbox that replaces observable events by<br />

controllable events. In our controller, we can achieve this by calling the controller’s<br />

ReceiveEvent method directly from the test code, rather than invoking it from an<br />

event handler.<br />

Figure 3.7 shows an NUnit test method that checks the typical run where the<br />

controller successfully resets, then polls, then computes the calibration factor. In<br />

more free ebooks download links at:<br />

http://www.ebook-x.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!