HTML and CSS Reference
In-Depth Information
So to create the appropriate unit tests, end the following code and comments
within the <script /> tag for your first unit test on the constructor.
test('calculator constructor', function(){
/**
* Specify how many assertions this test will run
* If assertions do not run for any reason, this
* test will fail
*/
expect(1);
/**
* You create a new calculator instance and set
* initial value to 10
*/
var calculator = new app.calculator(10);
/**
* You then assert that 10 is being held as the
* current result
*/
equal(calculator.getResult(), 10, 'the result should equal 10 with no
operation');
});
As you can see, the test method is part of QUnit, and accepts a description and
callback function. When the test executes, the callback function is called and
the test is executed. Within the test, you can see several methods. expect()
specifies how many assertions will run within the test. You also instantiate the
calculator class so that it can be used within the test. Any variables that are
created per test are destroyed and cannot be used in another test. They can,
however, be used within any assertions within the test. Finally, equal() is an
assertion. An assertion simply takes a result and checks to see whether the
resulting or returned value matches an expected value. In this instance, the
equal assertion checks to see whether the new calculator has 10 as the initial
result.
That's all there is to unit testing. There are a variety of assertions available. See
Table 9-2 for a list of the important ones.
 
Search WWH ::




Custom Search