Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

270 CHAPTER 6 Essential JavaScript and jQuery


FIGURE 6-3 uccessful first assertion and failed second assertionS

Consider the following test code that creates two Vehicle objects, but then replaces the
code in getInfo of the first Vehicle object with different code. Does this replace the code in
the second Vehicle object?
test("Function Replacement Test", function () {
expect(2);
var car1 = new Vehicle(2000, 'Ford', 'Fusion');
var car2 = new Vehicle(2010, 'BMW', 'Z4');
car1.getInfo = function () {
return 'This is a Car';
};
var expected = 'This is a Car';
var actual = car1.getInfo();
equal(actual, expected, 'Expected value: ' + expected +
' Actual value: ' + actual);
var expected = 'This is a Car';
var actual = car2.getInfo();
equal(actual, expected, 'Expected value: ' + expected +
' Actual value: ' + actual);
});

The test result is shown in Figure 6-4. The first assertion succeeded, which proves that the
function was successfully replaced on the first Vehicle object. The second assertion failed,
which proves that the second Vehicle object’s getInfo function was not replaced. Is that what
Free download pdf