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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

252 <strong>Model</strong>ing Objects<br />

}<br />

}<br />

void SetSalary(int x)<br />

{ this.salary = x; }<br />

public override void Initialize()<br />

{ this.salary = 0; }<br />

If you compare this version to the previous example, you will notice that we have<br />

introduced a C# instance field for salary. Also, instance methods have replaced some<br />

of the static methods for the actions.<br />

The method of instance creation differs as well. The Domain attribute that supplies<br />

parameter values for the CreateEmployee is given the string "new" as its argument.<br />

The N<strong>Model</strong> framework provides a parameter generator function for each class that<br />

inherits LabeledInstance.<br />

Both versions of this example have exactly the same possible traces. For example,<br />

Payroll1 and Payroll2 may both have the trace:<br />

CreateEmployee(Employee(1))<br />

SetSalary(Employee(1), 200)<br />

CreateEmployee(Employee(2))<br />

SetSalary(Employee(2), 400)<br />

DeleteEmployee(Employee(1))<br />

If you were to inspect the state, you would see that the modeling tool maintains<br />

a field map for you for the salary field. It also maintains state for the next object ID<br />

to be allocated.<br />

The keys of a field map created for an instance field contain all reachable instances<br />

of the class. At the end of the sample trace shown above, there is just one reachable<br />

object, Employee(2). An action like DeleteEmployee(Employee(1)) can eliminate<br />

an element in the salary field map because when Employee(1) is removed from the<br />

allEmployees field, it becomes unreachable.<br />

Note that actions of the model program may be instance methods. In this case the<br />

this parameter becomes the first parameter of the action. Appendix A.1.3 describes<br />

in more detail how the parameters of C# methods correspond to the parameters of<br />

actions of the model program.<br />

Also, the action vocabulary of a model program consists of distinct symbols.<br />

Overloading is not supported for action names. Action names should be chosen so<br />

that they are easy to understand and unambiguous. For example, we used CreateEmployee<br />

rather than the shorter name Create that would more conflict with action<br />

names associated with other data types.<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!